首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将rightBarButtonItem设置为粗体

将rightBarButtonItem设置为粗体
EN

Stack Overflow用户
提问于 2019-10-12 02:00:50
回答 2查看 75关注 0票数 0

我正在尝试设置一个导航按钮,使其显示为红色和半粗体。我设法更改了颜色,但在将其更改为半粗体时遇到了困难:

代码语言:javascript
复制
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。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-12 02:11:23

可以将style属性设置为.done以使UIBarButtonItem变为粗体。这将一直有效,直到苹果在新版本的iOS中改变done按钮的外观。

代码语言:javascript
复制
navigationItem.rightBarButtonItem?.style = .done
票数 1
EN

Stack Overflow用户

发布于 2019-10-12 02:23:27

要拥有完全自定义的栏按钮外观,您可以使用带有自定义视图的栏按钮项,如下所示

代码语言:javascript
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58346376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档