iPhone UITextField background color

2019-01-13 18:41发布

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?

7条回答
够拽才男人
2楼-- · 2019-01-13 19:12

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:

    UIView *textFieldShadeView=[[UIView alloc] init];
[textFieldShadeView setFrame:myTextFiled.frame];
textFieldShadeView.layer.cornerRadius=8.0f;
textFieldShadeView.layer.masksToBounds=YES;
textFieldShadeView.backgroundColor=[UIColor blueColor];
textFieldShadeView.alpha=0.3;
textFieldShadeView.userInteractionEnabled=NO;
[self.view addSubview:textFieldShadeView];
[textFieldShadeView release];

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.

查看更多
Rolldiameter
3楼-- · 2019-01-13 19:14

A dump of the view hierarchy reveals that the UITextField has a single subview of type UITextFieldRoundedRectBackgroundView, which in turn has 12 UIImageViews.

An older article by Erica Sadun shows an additional UILabel, which Apple apparently removed in later versions of the SDK.

Fiddling with the UIImageViews doesn't change much.

So the answer is: there's probably no way to change the background color.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-13 19:15

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:

[myTextField setBackgroundColor:[UIColor colorWithRed:255.0f/255.0f green:110.0f/255.0f blue:110.0f/255.0f alpha:1];

All TextField styles appear to work when you do this.

Also see: background color not working as expected

查看更多
太酷不给撩
5楼-- · 2019-01-13 19:15

You can do this:

textField.backgroundColor = [UIColor whiteColor];

In this case I'm doing it with white color but you can do it with another color for uiColor.

Hopes it helps

查看更多
混吃等死
6楼-- · 2019-01-13 19:18
...
textField.opaque = YES;
textField.backgroundColor=[UIColor blueColor];
textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES
查看更多
The star\"
7楼-- · 2019-01-13 19:20

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

textField.backgroundColor = backgroundColor;

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

#import <QuartzCore/QuartzCore.h>

and then set

textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES

for this feature to work

查看更多
登录 后发表回答