How to change the default text of Cancel Button which appears in the UISearchBar +iPhone

后端 未结 15 2024
慢半拍i
慢半拍i 2020-12-01 03:04

I am developing an Application where I wanted to change the text of Search String in the SearchBar. I wanted to change the text of Cancel Button Also which appears next to t

15条回答
  •  醉话见心
    2020-12-01 03:37

    Jeremytripp 's working Code in Swift

    I couldn't find the same code in Swift so I "translated" it myself:

    func searchDisplayControllerWillBeginSearch(controller: UISearchDisplayController) {
        self.searchDisplayController?.searchBar.showsCancelButton = true
        var cancelButton: UIButton
        var topView: UIView = self.searchDisplayController?.searchBar.subviews[0] as UIView
        for subView in topView.subviews {
            if subView.isKindOfClass(NSClassFromString("UINavigationButton")) {
            cancelButton = subView as UIButton
            cancelButton.setTitle("My Custom Title", forState: UIControlState.Normal)
            }
        }
    }
    

提交回复
热议问题