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

后端 未结 15 2010
慢半拍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:25

    Working short code in Swift 2.1 (iOS7-9 tested)

    @IBOutlet weak var searchBar: UISearchBar!
    func enableSearchBarCancelButton(enable: Bool, title: String? = nil) {
        searchBar?.showsCancelButton = enable
        if enable {
            if let _cancelButton = searchBar?.valueForKey("_cancelButton"),
                let cancelButton = _cancelButton as? UIButton {
                    cancelButton.enabled = enable //comment out if you want this button disabled when keyboard is not visible
                    if title != nil {
                        cancelButton.setTitle(title, forState: UIControlState.Normal)
                    }
            }
        }
    }
    

提交回复
热议问题