iOS Change the title of cancel button in UISearchBar

前端 未结 13 1889
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 10:35

I wish to change the title of the cancel button in iOS. I have been using this previously:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayCon         


        
13条回答
  •  长发绾君心
    2020-12-28 11:06

    For Swift 3.0

    This is working fine.

    func setSearchButtonText(text:String,searchBar:UISearchBar) {
    
        for subview in searchBar.subviews {
            for innerSubViews in subview.subviews {
                if let cancelButton = innerSubViews as? UIButton {
                    cancelButton.setTitleColor(UIColor.white, for: .normal)
                    cancelButton.setTitle(text, for: .normal)
                }
            }
        }
    
    }
    

    And call the method

    setSearchButtonText(text: "Done", searchBar: yourSearchBar)
    

    Here is the output

提交回复
热议问题