带有 BG 颜色和文本 Swift 3 的自定义 UIBarButtonItem
我的导航栏右侧有两个按钮.
I have two buttons on my right of my navigation bar.
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 带有背景颜色,并且该按钮具有圆角半径.如何添加它以使其看起来像:
I have created 2 buttons like this. But the profileButton I want is with background colour and having corner radius to that button. How to add it to make it look like :
忽略黑色部分.UIBarButton
将是黄色背景和圆角半径.
Ignore black part. UIBarButton
will be of yellow colour background and corner radius.
为此,您只有一个选择,您需要创建具有您想要的设计的 UIButton
实例,然后创建 UIBarButtonItem
来自该按钮的实例.
For that you have only one option you need to create UIButton
instance with design that you want after that create UIBarButtonItem
instance from that button.
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
设置它.
Now create the UIBarButtonItem
from that button and set it with the rightBarButtonItems
.
navigationItem.rightBarButtonItems = [addRightBarButtonWithImage(UIImage(named: "menu")!), UIBarButtonItem(customView: btnProfile)]