隱藏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;
    }

 

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