ios iPhone UITextField 背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1971598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:43:42  来源:igfitidea点击:

iPhone UITextField background color

iosuitextfieldbackground-color

提问by Stephen Joy

I am unable to control the background color of a UITextFieldwith a borderStyle= UITextBorderStyleRoundedRect. With this border style the backgroundColorproperty only seems to control a very narrow line along the inner edge of the rounded rectangle. The rest of the field remains white.

我无法控制的背景色UITextFieldborderStyle= UITextBorderStyleRoundedRect。使用这种边框样式,该backgroundColor属性似乎只能控制沿圆角矩形内边缘的一条非常窄的线。该领域的其余部分保持白色。

However, if the borderStyleis set to UIBorderStyle=UITextBorderStyleBezelthen the entire background of the UITextFieldis controlled by its backgroundColorproperty.

但是,如果borderStyle设置为UIBorderStyle=UITextBorderStyleBezel则整个背景都UITextField由其backgroundColor属性控制。

Is this a feature? Is there a way to control the backgroundColorof a UITextFieldwith a borderStyle=UITextBorderStyleRoundedRect?

这是一个功能吗?有没有办法控制backgroundColorUITextField一个borderStyle=UITextBorderStyleRoundedRect

回答by Peter Johnson

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.

要更改 UITextField 中的背景颜色,您首先需要在 Interface Builder 中或以编程方式使用与“圆形”样式(例如“无边框”样式)不同的文本字段样式。

You can then easily change the background colour with

然后,您可以轻松更改背景颜色

textField.backgroundColor = backgroundColor;

where textField is your UITextField, and backgroundColor is a UIColor.

其中 textField 是您的 UITextField,backgroundColor 是 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

使此功能正常工作

回答by Jacob Jennings

colored UITextField using overlay

使用叠加层的彩色 UITextField

The other answers don't have the shadows present on a UITextField with Rounded Rectangle style. After trying many options I finally just placed a UIView over the UITextField, with an identical frame and auto resize mask, set the alpha to 0.3, and set the background to a blue color. Then I used the snippet from Peter Johnson's answer to clip the rounded edges off the colored overlay view. Also, uncheck the 'User Interaction Enabled' checkbox in IB to allow touch events to cascade down to the UITextField underneath. Looks perfect now.

其他答案在圆角矩形样式的 UITextField 上没有阴影。在尝试了许多选项后,我终于在 UITextField 上放置了一个 UIView,具有相同的框架和自动调整大小的蒙版,将 alpha 设置为 0.3,并将背景设置为蓝色。然后我使用 Peter Johnson 的回答中的片段将圆形边缘从彩色叠加视图中剪掉。此外,取消选中 IB 中的“启用用户交互”复选框,以允许触摸事件向下级联到下方的 UITextField。现在看起来很完美。

Side effect: your text will also be colorized (doesn't matter if it's black)

副作用:你的文字也会被着色(如果是黑色也没关系)

#import <QuartzCore/QuartzCore.h>

colorizeOverlayView.layer.cornerRadius = 6.0f;
colorizeOverlayView.layer.masksToBounds = YES;

回答by bndouglas

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:

当使用 colorWithRed:green:blue: 设置 backgroundColor 时,颜色应该是浮动的并且应该是 1 的一小部分。例如:

[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.

执行此操作时,所有 TextField 样式似乎都有效。

Also see: background color not working as expected

另请参阅:背景颜色未按预期工作

回答by Can Berk Güder

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

视图层次结构的转储显示UITextField有一个类型为 的子视图UITextFieldRoundedRectBackgroundView,而该子视图有 12UIImageView秒。

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

一个旧的文章埃丽卡丧盾显示了一个额外的UILabel,苹果公司在SDK的更新版本显然删除。

Fiddling with the UIImageViews doesn't change much.

摆弄UIImageViews 并没有太大变化。

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

所以答案是:可能没有办法改变背景颜色。

回答by MyCSharpCorner

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!

Jacob 的答案是这里最好的答案,因为它允许您将阴影保留在 RoundedRect 文本框下方,因此 Jacob 为 +1!

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.

其中 myTextFiled 是您尝试更改背景颜色的圆形矩形文本字段。执行上述操作,您将获得 Jacob 的蓝色文本字段以及适当的阴影。

回答by Iustin Octav

...
textField.opaque = YES;
textField.backgroundColor=[UIColor blueColor];
textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES

回答by yuukan

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.

在这种情况下,我用白色来做,但你可以用另一种颜色来做uiColor.

Hopes it helps

希望有帮助