将 rightBarButtonItem 设置为粗体

Making rightBarButtonItem bold

我正在尝试将导航按钮设置为显示为红色和半粗体。我设法更改了颜色,但无法将其更改为半粗体:

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