我的导航栏右侧有两个按钮。
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 具有背景颜色并且该按钮具有圆角半径。如何添加它以使其看起来像:
忽略黑色部分。 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/