Cannot remove border of UITextField dynamically

2020-07-18 05:35发布

问题:

I want to remove the border of UITextField dynamically.

I tried [stringTextField setBorderStyle:UITextBorderStyleNone];

but nothing happened. Any idea?

回答1:

Is the TextField already displayed in the view when this happens? If so, you (probably) need to execute the following:

[stringTextField setBorderStyle:UITextBorderStyleNone];
[stringTextField setNeedsDisplay];

in order for the view to redraw the TextField, sans border. Note that there's no guarantee the system will immediately redraw the textField. You're indicating to the system that you'd like the field to be redrawn.



回答2:

With an existing UITextField I found that this worked:

[textField setEnabled:NO];
[textField setBorderStyle:UITextBorderStyleNone];

while this did not (the border remained in the view):

[textField setBorderStyle:UITextBorderStyleNone];
[textField setEnabled:NO];


回答3:

Try this ones.

textField.borderStyle = UITextBorderStyleRoundedRect;
textField.borderStyle = UITextBorderStyleNone;