斯威夫特 4.2
UIBarButtonItem 使用属性文本:
func createCancelButton() {
guard let font = UIFont(name: "OpenSans", size: 12) else { return }
let cancelButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(cancelTapped))
cancelButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.blue, NSAttributedString.Key.font : font], for: .normal)
navigationItem.leftBarButtonItem = cancelButton
}
@objc func cancelTapped() {
print("cancelTapped")
}
UIBarButtonItem 使用纯文本:
func createCancelButton() {
let cancelButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(cancelTapped))
cancelButton.tintColor = UIColor.blue
navigationItem.leftBarButtonItem = cancelButton
}
@objc func cancelTapped() {
print("cancelTapped")
}