且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-08-09 23:18:36

为此,您只有一个选择,您需要创建具有您想要的设计的 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)]