I am unable to control the background color of a UITextField
with a borderStyle= UITextBorderStyleRoundedRect
. With this border style the backgroundColor
property only seems to control a very narrow line along the inner edge of the rounded rectangle. The rest of the field remains white.
However, if the borderStyle
is set to UIBorderStyle=UITextBorderStyleBezel
then the entire background of the UITextField
is controlled by its backgroundColor
property.
Is this a feature? Is there a way to control the backgroundColor
of a UITextField
with a borderStyle=UITextBorderStyleRoundedRect
?
Jacob's answer was the best answer here since it allows you to keep the shadows underneath the RoundedRect text boxes, so +1 for Jacob!
To elaborate on his solution, yo need to do this:
Where myTextFiled is the rounded rect text field you are trying change the background color for. Do the above and you will get Jacob's bluish text field along with the appropriate shadows.
A dump of the view hierarchy reveals that the
UITextField
has a single subview of typeUITextFieldRoundedRectBackgroundView
, which in turn has 12UIImageView
s.An older article by Erica Sadun shows an additional
UILabel
, which Apple apparently removed in later versions of the SDK.Fiddling with the
UIImageView
s doesn't change much.So the answer is: there's probably no way to change the background color.
This is much easier than we all thought.
When setting the backgroundColor using colorWithRed:green:blue:, the colors should be floats and should be a fraction of 1. For example:
All TextField styles appear to work when you do this.
Also see: background color not working as expected
You can do this:
In this case I'm doing it with white color but you can do it with another color for
uiColor
.Hopes it helps
To change the background color in a UITextField you first need to use a different style of text field to the "rounded" style (such as the "no border" style) either in Interface Builder or programmatically.
You can then easily change the background colour with
where textField is your UITextField, and backgroundColor is a UIColor.
As a further tip- if you wish to restore the rounded corners appearance, you will first need to
and then set
for this feature to work