Change title of a navigation bar button item

那年仲夏 提交于 2019-12-07 08:22:03

问题


let button = UIButton()
        button.setImage(UIImage(named: "coin_icon"), forState: UIControlState.Normal)
        button.addTarget(self, action:#selector(Profile.goCoin), forControlEvents: UIControlEvents.TouchDragInside)
        button.frame=CGRectMake(0, 0, 30, 30)
        let barButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = barButton

My code is this to add a bar button on navigation bar.

However, i need to change the title of button in a function

func changetitle() {
self.navigationItem.rightBarButtonItem?.title = "Change"
}

I tried this one but didn't work.

How can i change the title of this button?


回答1:


You need to access the UIButton from the UIBarButtonItem and change the title of the UIButton.

func changetitle() {
    let item = self.navigationItem.rightBarButtonItem!
    let button = item.customView as! UIButton
    button.setTitle("Change", for: .normal)
}


来源:https://stackoverflow.com/questions/37350333/change-title-of-a-navigation-bar-button-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!