iPhone UITextField background color

前端 未结 7 1086
死守一世寂寞
死守一世寂寞 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: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 
    
    colorizeOverlayView.layer.cornerRadius = 6.0f;
    colorizeOverlayView.layer.masksToBounds = YES;
    

提交回复
热议问题