带有 BG 颜色和文本 Swift 3 的自定义 UIBarButtonItem [英] Custom UIBarButtonItem With BG Colour and Text Swift 3

查看:33
本文介绍了带有 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)]

这篇关于带有 BG 颜色和文本 Swift 3 的自定义 UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆