ban*_*anu 6 iphone uibutton ios ios5 ios5.1
在我的应用程序中,我添加了一个UISearchBar.
我的目的是使UISearch栏"X按钮"(UITextField中的清除按钮)始终可见.
我尝试使用下面的代码尝试使"X按钮"始终可见.但是,它不起作用.如果我设置tf.clearButtonMode = UITextFieldViewModeNever,uitextfield中的清除按钮不显示.我不确定是什么问题?
我真的很感激任何人的帮助.为什么这不起作用?
for (UIView* v in searchBar.subviews)
{
if ( [v isKindOfClass: [UITextField class]] )
{
UITextField *tf = (UITextField *)v;
tf.delegate = self;
tf.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
如果文本长度等于0,我想始终显示清除按钮
您需要为清除按钮创建自定义UIButton
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[clearButton setImage:img forState:UIControlStateNormal];
[clearButton setFrame:frame];
[clearButton addTarget:self action:@selector(clearTextField:) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeAlways; //can be changed to UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing
[textField setRightView:clearButton];
Run Code Online (Sandbox Code Playgroud)
你可以在西安做。我附上屏幕截图。

并以编程方式
myUITextField.clearButtonMode = UITextFieldViewModeAlways;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8155 次 |
| 最近记录: |