简体   繁体   English

iPhone UITextField 背景色

[英]iPhone UITextField background color

I am unable to control the background color of a UITextField with a borderStyle= UITextBorderStyleRoundedRect .我无法使用borderStyle= UITextBorderStyleRoundedRect控制UITextField的背景颜色。 With this border style the backgroundColor property only seems to control a very narrow line along the inner edge of the rounded rectangle.使用这种边框样式, backgroundColor属性似乎只控制沿着圆角矩形内边缘的一条非常窄的线。 The rest of the field remains white.该字段的 rest 保持白色。

However, if the borderStyle is set to UIBorderStyle=UITextBorderStyleBezel then the entire background of the UITextField is controlled by its backgroundColor property.但是,如果borderStyle设置为UIBorderStyle=UITextBorderStyleBezel ,则UITextField的整个背景由其backgroundColor属性控制。

Is this a feature?这是一个功能吗? Is there a way to control the backgroundColor of a UITextField with a borderStyle=UITextBorderStyleRoundedRect ?有没有办法用borderStyle=UITextBorderStyleRoundedRect控制UITextFieldbackgroundColor颜色?

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 使这个功能工作

使用叠加颜色的UITextField

The other answers don't have the shadows present on a UITextField with Rounded Rectangle style. 其他答案在具有圆角矩形样式的UITextField上没有阴影。 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. 在尝试了很多选项之后,我终于在UITextField上放置了一个UIView,使用相同的框架和自动调整大小的蒙版,将alpha设置为0.3,并将背景设置为蓝色。 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. 此外,取消选中IB中的“启用用户交互”复选框,以允许触摸事件级联到下面的UITextField。 Looks perfect now. 现在看起来很完美

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;

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 另请参阅: 背景颜色未按预期工作

A dump of the view hierarchy reveals that the UITextField has a single subview of type UITextFieldRoundedRectBackgroundView , which in turn has 12 UIImageView s. 视图层次结构的转储显示UITextField具有UITextFieldRoundedRectBackgroundView类型的单个子视图,该子视图又具有12个UIImageView

An older article by Erica Sadun shows an additional UILabel , which Apple apparently removed in later versions of the SDK. Erica Sadun的一篇较旧的文章展示了一个额外的UILabel ,Apple显然已在SDK的更高版本中删除了该文章。

Fiddling with the UIImageView s doesn't change much. 摆弄UIImageView并没有太大变化。

So the answer is: there's probably no way to change the background color. 所以答案是:可能无法改变背景颜色。

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! 雅各布的答案是最好的答案,因为它允许你保持在RoundedRect文本框下方的阴影,所以为雅各布+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. myTextFiled是圆角矩形文本字段,您尝试更改背景颜色。 Do the above and you will get Jacob's bluish text field along with the appropriate shadows. 执行上述操作,您将获得雅各布的蓝色文本字段以及适当的阴影。

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

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 希望它有所帮助

When I encountered this problem, my approach was to set the background of my UITextField to clear and embed it within a larger UI View, which has the color and border that I desire.当我遇到这个问题时,我的方法是将我的 UITextField 的背景设置为清除并将其嵌入到更大的 UI 视图中,该视图具有我想要的颜色和边框。 The following swift code does that.以下 swift 代码就是这样做的。 With this code, you can set the background color of the container view to whatever you want.使用此代码,您可以将容器视图的背景颜色设置为您想要的任何颜色。 For example, if you set the textContainer background color to yellow, it would look like this ( though I wouldn't actually recommend this shade, it is just for example purposes ):例如,如果您将 textContainer 背景颜色设置为黄色,它看起来像这样(虽然我实际上不推荐这种色调,它只是为了示例目的):

在此处输入图像描述

If you show view frames in the Xcode debugger (image below) you see that the actual text field is a smaller rectangle inside what appears to be the text field (but is actually the container view).如果您在 Xcode 调试器(下图)中显示视图框架,您会看到实际的文本字段是一个较小的矩形,位于看似文本字段(但实际上是容器视图)的内部。 Note that Apple actually takes this same approach in the UIAlertController.请注意,Apple 实际上在 UIAlertController 中采用了相同的方法。

在此处输入图像描述

let field = UITextField()
field.font = UIFont.systemFont(ofSize: 13, weight: .regular)
field.backgroundColor = .clear

let textContainer = UIView()
textContainer.addSubview(field)
textContainer.isUserInteractionEnabled = true
textContainer.backgroundColor = .yellow
textContainer.layer.masksToBounds = true
textContainer.layer.borderWidth = 0.25
textContainer.layer.borderColor = myBorderColor
textContainer.layer.cornerRadius = 7.0
    
NSLayoutConstraint.activate([
    textContainer.heightAnchor.constraint(equalToConstant: 30.67),
    field.centerYAnchor.constraint(equalTo: textContainer.centerYAnchor),
    field.trailingAnchor.constraint(equalTo: textContainer.trailingAnchor, constant: -7.0),
    field.leadingAnchor.constraint(equalTo: textContainer.leadingAnchor, constant: 7.0),
 ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM