怎么让UISearchBar中的Xbutton(clear button)一直显示。
for (UIView* v in searchBar.subviews)
{
if ( [v isKindOfClass: [UITextField class]] )
{
UITextField *tf = (UITextField *)v;
tf.delegate = self;
tf.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
我试过了没有用,如果我设置tf.clearButtonMode = UITextFieldViewModeNever
,xbutton就不显示了。
应该怎么办?
我需要在文本长度等于0的状态一直显示Xbutton。
需要再创建一个自定义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];
这是searchbar的默认状态,如果文本区域是空的不需要去按它