隐藏UISearchBar中的clearButton

随着iOS13的发布,老APP中的一些代码无法正常运行,遇到一个正常模式下的UISearchBart,隐藏clearButton的方法无法运行,方法如下:

   //SearchBar only have one subview (UIView)
        UIView *subview = [[self.searchBar subviews] firstObject];
        //There are three sub subviews (UISearchBarBackground, UINavigationButton, UISearchBarTextField)
        for (UIView *subsubview in subview.subviews) {
            //The UISearchBarTextField class is a UITextField. We can't use UISearchBarTextField directly here.
            if ([subsubview isKindOfClass: [UITextField class]]) {
                    [(UITextField *)subsubview setClearButtonMode:UITextFieldViewModeNever];
            }
        }

改为如下代码后,iOS13运行正常,但是iOS13之前的环境又奔溃:

self.searchBar.searchTextField.clearButtonMode = UITextFieldViewModeNever;

只好加上对系统版本的判断,先这样,有好的方法再改吧:

//iOS13以后无法用原有方法隐藏searchBar的clearButton
    if ([[[UIDevice currentDevice] systemVersion] floatValue]<13.0){
        //SearchBar only have one subview (UIView)
        UIView *subview = [[self.searchBar subviews] firstObject];
        //There are three sub subviews (UISearchBarBackground, UINavigationButton, UISearchBarTextField)
        for (UIView *subsubview in subview.subviews) {
            //The UISearchBarTextField class is a UITextField. We can't use UISearchBarTextField directly here.
            if ([subsubview isKindOfClass: [UITextField class]]) {
                    [(UITextField *)subsubview setClearButtonMode:UITextFieldViewModeNever];
            }
        }
    }
    else{
        self.searchBar.searchTextField.clearButtonMode = UITextFieldViewModeNever;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章