ios - 带有 BG 颜色和文本的自定义 UIBarButtonItem Swift 3

标签 ios swift swift3 uibarbuttonitem

我的导航栏右侧有两个按钮。

extension UIViewController {

func setNavigationBarItem() {

    let profileButton   = UIBarButtonItem(title: "?", style: .plain, target: self, action: #selector(didTapProfileButton(_:)))
    navigationItem.rightBarButtonItems = [addRightBarButtonWithImage(UIImage(named: "menu")!), profileButton]

    self.slideMenuController()?.removeRightGestures()
    self.slideMenuController()?.addRightGestures()
    }
}

我已经创建了 2 个这样的按钮。但是我想要的 profileButton 具有背景颜色并且该按钮具有圆角半径。如何添加它以使其看起来像:

enter image description here

忽略黑色部分。 UIBarButton 将具有黄色背景和角半径。

最佳答案

为此,您只有一个选择,您需要创建具有您想要的设计的 UIButton 实例,然后从该按钮创建 UIBarButtonItem 实例。

let btnProfile = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
btnProfile.setTitle("SY", for: .normal)
btnProfile.backgroundColor = UIColor.yellow
btnProfile.layer.cornerRadius = 4.0
btnProfile.layer.masksToBounds = true

现在从该按钮创建 UIBarButtonItem 并使用 rightBarButtonItems 设置它。

navigationItem.rightBarButtonItems = [addRightBarButtonWithImage(UIImage(named: "menu")!), UIBarButtonItem(customView: btnProfile)]

关于ios - 带有 BG 颜色和文本的自定义 UIBarButtonItem Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41425460/

相关文章:

iphone - 编写 NSOperation 友好的方法

ios - swift ActivityIndi​​cator 自动停止动画

arrays - 填充 UITableViewController

ios - 将 CLLocationCoordinate2D 数组保存到核心数据?

ios - Swift - 有没有办法在相机处于事件状态时呈现 AlertController?

ios - 如何使用 Swift 更新 UITableView?

ios - TableView 调整父 View 大小 iOS

ios - 如何在 Xcode 4 中重命名 xib 文件?

ios - 可选绑定(bind)与尝试?并作为?仍然产生可选类型

iphone - 如何在iphone中实现google搜索应用