clear button in UISearchBar

喜你入骨 提交于 2019-12-08 05:04:23

问题


Hi can I remove clear button (X button)present in UISearchBar?

I didn't find any delegate for that. There is one for cancel button.

Thanks


回答1:


You can not directly access the clearButton of UISearchBar. You have to loop through the subviews of the UISearchBar to find the UITextField, and set its clearButtonMode property to UITextFieldViewModeNever.

Note: This is not a permanent solution because this may not work in future if the implementation of UISearchBar changes by the subsequent iOS updates.




回答2:


You need to get the textField of the Search Bar

UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;

use in - searchBarTextDidBeginEditing method.

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
 {

UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;


 }


来源:https://stackoverflow.com/questions/6646434/clear-button-in-uisearchbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!