change text of button and disable button in iOS

2019-01-30 20:41发布

How do you change the text of the button and disable a button in iOS?

标签: ios button text
8条回答
迷人小祖宗
2楼-- · 2019-01-30 21:14

SWIFT 4 with extension

set:

// set button label for all states
extension UIButton {
    public func setAllStatesTitle(_ newTitle: String){
        self.setTitle(newTitle, for: .normal)
        self.setTitle(newTitle, for: .selected)
        self.setTitle(newTitle, for: .disabled)
    }
}

and use:

yourBtn.setAllStatesTitle("btn title")
查看更多
The star\"
3楼-- · 2019-01-30 21:25

If somebody, who is looking for a solution in Swift, landed here, it would be:

myButton.isEnabled = false // disables
myButton.setTitle("myTitle", for: .normal) // sets text

Documentation: isEnabled, setTitle.

Older code:

myButton.enabled = false // disables
myButton.setTitle("myTitle", forState: UIControlState.Normal) // sets text
查看更多
登录 后发表回答