Styling the cancel button in a UISearchBar

前端 未结 21 1416
-上瘾入骨i
-上瘾入骨i 2020-12-02 09:13

I have a UISearchBar that has a cancel button (it\'s displayed using -(void)setShowsCancelButton:animated). I\'ve changed the tintColor of the sear

21条回答
  •  心在旅途
    2020-12-02 09:53

    The best way to style the cancelButton is without using UIAppearance is like this it is for Swift5 iOS13 and it works best with UISearchResultController.searchBar too

    extension UISearchBar {
        func changeSearchBarAppearance(appearance: MyAppearance) {
            self.barTintColor = appearance.searchbar.barTintColor
            self.tintColor  =  appearance.searchbar.tintColor
            if let textField = self.subviews.first?.subviews.last?.subviews.first {
                textField.tintColor = .black
            }
        }
    }
    

    setting serachBar tintColor will set the tintColor of all items including the cancelButton but with this the blinker in the searchField will also be set with the same tintColor so find the textfield and set its tintColor will solve the blinker issue

提交回复
热议问题