问题描述
- 在UisearchBar中一直显示Xbutton
-
怎么让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的默认状态,如果文本区域是空的不需要去按它
时间: 2024-12-22 02:33:12