Styling the cancel button in a UISearchBar

前端 未结 21 1373
-上瘾入骨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:14

    extension UISearchBar {
    var cancelButton : UIButton? {
        let topView: UIView = self.subviews[0] as UIView
    
        if let pvtClass = NSClassFromString("UINavigationButton") {
            for v in topView.subviews {
                if v.isKind(of: pvtClass) {
                    return v as? UIButton
                }
            }
        }
    
        return nil
    }
    }
    

提交回复
热议问题