我正在尝试将导航按钮设置为显示为红色和半粗体。我设法更改了颜色,但是在将其更改为半粗体时遇到了麻烦:
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Sign Up", style: .plain, target: self, action: #selector(signIn))
navigationItem.rightBarButtonItem?.tintColor = .red
UIBarItem.appearance().setTitleTextAttributes(
[
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .semibold)
],
for: .normal)
我先尝试了rightBarButtonItem.appearance,但这似乎不是一个选择。
我正在使用Swift 4.2。
您可以将style
属性设置为.done
以使UIBarButtonItem
加粗。这将一直有效,直到Apple更改完成按钮在新版iOS中的外观。
navigationItem.rightBarButtonItem?.style = .done
具有完全自定义的条形按钮外观,您可以像这样使用具有自定义视图的条形按钮项目
let doneButton: UIButton = UIButton (type: UIButton.ButtonType.custom)
doneButton.setTitle("Done", for: .normal)
doneButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
doneButton.setTitleColor(.blue, for: .normal)
doneButton.addTarget(self, action: #selector(self.doneBarButtonTapped(sender:)), for: UIControl.Event.touchUpInside)
doneButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let barButton = UIBarButtonItem(customView: doneButton)
navigationItem.rightBarButtonItem = barButton