iPhone UITextField background color

前端 未结 7 1033
死守一世寂寞
死守一世寂寞 2020-12-05 05:17

I am unable to control the background color of a UITextField with a borderStyle= UITextBorderStyleRoundedRect. With this border style the ba

相关标签:
7条回答
  • 2020-12-05 05:30
    ...
    textField.opaque = YES;
    textField.backgroundColor=[UIColor blueColor];
    textField.layer.cornerRadius=8.0f;
    textField.layer.masksToBounds=YES
    
    0 讨论(0)
  • 2020-12-05 05:31

    colored UITextField using overlay

    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.

    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;
    
    0 讨论(0)
  • 2020-12-05 05:39

    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

    0 讨论(0)
  • 2020-12-05 05:43

    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.

    0 讨论(0)
  • 2020-12-05 05:47

    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

    0 讨论(0)
  • 2020-12-05 05:48

    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

    0 讨论(0)
提交回复
热议问题