Nit*_*esh 4 uibarbuttonitem ios swift swift3
我的导航栏右侧有两个按钮.
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()
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了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
Run Code Online (Sandbox Code Playgroud)
现在UIBarButtonItem从该按钮创建并使用该按钮进行设置rightBarButtonItems.
navigationItem.rightBarButtonItems = [addRightBarButtonWithImage(UIImage(named: "menu")!), UIBarButtonItem(customView: btnProfile)]
Run Code Online (Sandbox Code Playgroud)
let profileButton = UIButton()
profileButton.frame = CGRect(x:0, y:0, width:30, height:30)
profileButton.setTitle("SY", for: .normal)
profileButton.setTitle("SY", for: .highlighted)
profileButton.backgroundColor = UIColor.yellow
profileButton.layer.cornerRadius = 5.0
profileButton.addTarget(self, action: #selector(didTapProfileButton(_:)), for: .touchUpInside)
let rightBarButton = UIBarButtonItem(customView: profileButton)
self.navigationItem.rightBarButtonItem = rightBarButton
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4248 次 |
| 最近记录: |