当我使用 customView 构造函数创建 UIBarButtonItem 时,它会获取设置了特定属性的 UIButton,那么在创建 UIBarButtonItem 对象后如何访问该属性?
var uiBarButtonItem: UIBarButtonItem {
let button = UIButton(type: .system)
button.setImage(UIImage(named: "image-name"), for: .normal)
button.setTitle("My Button Title", for: .normal)
button.sizeToFit()
button.addTarget(self, action: #selector(aFuncName), for: .touchUpInside)
return UIBarButtonItem(customView: button)
}
它应该像下面的伪代码一样工作:
uiBarButtonItem.button.titleLabel.text
// should return "My Button Title"
谢谢!
最佳答案
您需要访问 UIBarButtonItem
的 customView 属性。
if let barItem = self.navigationItem.leftBarButtonItem, //Access rightBarButtonItem if you have set right item
let btn = barItem.customView as? UIButton {
btn.setTitle("New", for: .normal)
}
关于swift3 - 如何在传递到 UIBarButtonItem-customView 构造函数后获取/设置按钮的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42959674/