Styling the cancel button in a UISearchBar

前端 未结 21 1376
-上瘾入骨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 10:08

    You can use UIAppearance to style the cancel button without iterating subviews of the UISearchBar, but the UIButton header does not currently have any methods annotated with UI_APPEARANCE_SELECTOR.

    EDIT: Drill down the subviews till you get that cancel button

    But this usually returns nil until searchBar.setShowsCancelButton(true, animated: true) is called.

    extension UISearchBar {
    
    var cancelButton : UIButton? {
        if let view = self.subviews.first {
            for subView in view.subviews {
                if let cancelButton = subView as? UIButton {
                    return cancelButton
                }
            }
        }
        return nil
    }
    }
    

提交回复
热议问题