Set background color to multiple textfields

Hi, is there a way to set background color to multiple textfield?



In my app i have don it like this:

Textfield1.background = UIIColor(redcolor)
Textfield2.background = UIIColor(redcolor)
Textfield3.background = UIIColor(redcolor)



It works fine, but is there a simpler way to assign a background color to all those 3 textfields?

Accepted Answer

You can write something like this:

        [Textfield1, Textfield2, Textfield3].forEach{$0.backgroundColor = UIColor.redColor()}

Thanks!

Remember, all the elements need to be of the same reference type.

If the text fields are defined in IB then you could make an outlet collection for them and loop or do the forEach thing on that without having to list the specific fields in code. UIKit would create the array for you.

Set background color to multiple textfields
 
 
Q