Hide UISearchBar clear text button

前端 未结 15 1790
时光说笑
时光说笑 2020-12-31 05:31

I would like to know how to hide or not display the UISearchBar cross that appears in the textField fo the UISearchBar

I have

15条回答
  •  暖寄归人
    2020-12-31 05:58

    Swift 4

    Adding to Alexander's answer and block user interaction on clear button:

    To hide button:

    searchBar.setImage(UIImage(), for: .clear, state: .normal)
    

    To disable user interaction on the clear button, simply subclass UISearchBar

    class CustomSearchBar: UISearchBar {
    
        override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
            let view = super.hitTest(point, with: event)
            if view is UIButton {
                return view?.superview // this will pass-through all touches that would've been sent to the button
            }
            return view
        }
    }
    

提交回复
热议问题