导航栏中的 UIBarButtonItem 以编程方式?

Posted

技术标签:

【中文标题】导航栏中的 UIBarButtonItem 以编程方式?【英文标题】:UIBarButtonItem in navigation bar programmatically? 【发布时间】:2015-07-13 09:43:06 【问题描述】:

我一直在寻找这个解决方案一段时间,但没有找到任何解决方案。 例如,一种解决方案是

 self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Stop, target: self, action: nil), animated: true)

此代码将添加一个带有“停止”图像的按钮。就像这样,还有“搜索”、“刷新”等其他解决方案。但是如果我想以编程方式添加带有我想要的图像的按钮怎么办?

【问题讨论】:

【参考方案1】:

不设置按钮边框的自定义按钮图片:

您可以使用init(image: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?) 使用指定的图像和其他属性初始化新项目。

let button1 = UIBarButtonItem(image: UIImage(named: "imagename"), style: .plain, target: self, action: Selector("action")) // action:#selector(Class.MethodName) for swift 3
self.navigationItem.rightBarButtonItem  = button1

检查这个Apple Doc. reference


UIBarButtonItem 与使用按钮框架的自定义按钮图像

Swift 3.0

    let btn1 = UIButton(type: .custom)
    btn1.setImage(UIImage(named: "imagename"), for: .normal)
    btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    btn1.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)
    let item1 = UIBarButtonItem(customView: btn1)

    let btn2 = UIButton(type: .custom)
    btn2.setImage(UIImage(named: "imagename"), for: .normal)
    btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    btn2.addTarget(self, action: #selector(Class.MethodName), for: .touchUpInside)
    let item2 = UIBarButtonItem(customView: btn2)  

    self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)

适用于Swift 2.0 及以上版本

let btnName = UIButton()
btnName.setImage(UIImage(named: "imagename"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)

//.... Set Right/Left Bar Button item
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = btnName
self.navigationItem.rightBarButtonItem = rightBarButton

或者干脆用init(customView:)like

 let rightBarButton = UIBarButtonItem(customView: btnName)
 self.navigationItem.rightBarButtonItem = rightBarButton

对于系统 UIBarButtonItem

let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
self.navigationItem.rightBarButtonItem = camera

设置超过 1 个项目使用rightBarButtonItems 或左侧leftBarButtonItems

let btn1 = UIButton()
btn1.setImage(UIImage(named: "img1"), forState: .Normal)
btn1.frame = CGRectMake(0, 0, 30, 30)
btn1.addTarget(self, action: Selector("action1:"), forControlEvents: .TouchUpInside)
let item1 = UIBarButtonItem()
item1.customView = btn1

let btn2 = UIButton()
btn2.setImage(UIImage(named: "img2"), forState: .Normal)
btn2.frame = CGRectMake(0, 0, 30, 30)
btn2.addTarget(self, action: Selector("action2:"), forControlEvents: .TouchUpInside)
let item2 = UIBarButtonItem()
item2.customView = btn2

self.navigationItem.rightBarButtonItems = [item1,item2]

使用setLeftBarButtonItemsetRightBarButtonItem

let btn1 = UIButton()
btn1.setImage(UIImage(named: "img1"), forState: .Normal)
btn1.frame = CGRectMake(0, 0, 30, 30)
btn1.addTarget(self, action: Selector("action1:"), forControlEvents: .TouchUpInside)
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: btn1), animated: true);

对于 swift >= 2.2 操作应该是 #selector(Class.MethodName) ... 例如btnName.addTarget(self, action: #selector(Class.MethodName), forControlEvents: .TouchUpInside)

【讨论】:

很好的答案。只想指出 Selector 方法已更新为 let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: #selector(Class.MethodName)) 我发现如果你想让你的按钮也包含文本,你必须指定 CGRect 部分。有一些围绕 SO 浮动的示例未指定这一点。 感谢您的完美回答。为swift 4添加更新也不错。【参考方案2】:

使用Swift 4Swift 4.2 会更容易

在您的 ViewDidLoad 方法中,定义您的按钮并将其添加到导航栏。

override func viewDidLoad() 
    super.viewDidLoad()

    let logoutBarButtonItem = UIBarButtonItem(title: "Logout", style: .done, target: self, action: #selector(logoutUser))
    self.navigationItem.rightBarButtonItem  = logoutBarButtonItem


那么你需要定义你在动作参数中提到的函数,如下所示

@objc func logoutUser()
     print("clicked")

您需要添加 @objc 前缀,因为它仍在使用旧的东西(目标 C)。

【讨论】:

【参考方案3】:

只需使用 customView 设置 UIBarButtonItem

例如:

  var leftNavBarButton = UIBarButtonItem(customView:yourButton)
  self.navigationItem.leftBarButtonItem = leftNavBarButton

或使用setFunction:

  self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: yourButton), animated: true);

【讨论】:

我赞成您的回答,因为您的代码也可以正常工作,但 Bhavin 的代码是一次性解决方案,所以我会接受他的回答。感谢您的贡献伙伴:)【参考方案4】:

我只是偶然发现了这个问题,这里是 Swift 3 和 ios 10 的更新:

let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "test.png"), style: .plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem  = testUIBarButtonItem

这肯定比使用所有属性创建 UIButton 然后将 customView 添加到 UIBarButtonItem 快得多。

如果您想将图像的颜色从默认的蓝色更改为例如白色,您可以随时更改色调颜色:

test.tintColor = UIColor.white()

PS 您显然应该为您的应用更改选择器等 :)

【讨论】:

【参考方案5】:

在 Swift 3.0+ 中,UIBarButtonItem 以编程方式设置如下:

   override func viewDidLoad() 
        super.viewDidLoad()
        let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "test.png"), style: .plain, target: self, action: #selector(self.clickButton))
        self.navigationItem.rightBarButtonItem  = testUIBarButtonItem
    

   @objc func clickButton()
            print("button click")
     

【讨论】:

【参考方案6】:

使用原始图像设置 LeftBarButton。

let menuButton = UIBarButtonItem(image: UIImage(named: "imagename").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(classname.functionname))
self.navigationItem.leftBarButtonItem  = menuButton

【讨论】:

【参考方案7】:

iOS 11

使用约束设置自定义按钮:

let buttonWidth = CGFloat(30)
let buttonHeight = CGFloat(30)

let button = UIButton(type: .custom)
button.setImage(UIImage(named: "img name"), for: .normal)
button.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
button.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true
button.heightAnchor.constraint(equalToConstant: buttonHeight).isActive = true

self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: button)

【讨论】:

【参考方案8】:

适用于 Swift 5+

let searchBarButtonItem = UIBarButtonItem(image: UIImage(named: "searchIcon"), style: .plain, target: self, action: #selector(onSearchButtonClicked))
        self.navigationItem.rightBarButtonItem  = searchBarButtonItem

@objc func onSearchButtonClicked(_ sender: Any)
    print("SearchButtonClicked")

【讨论】:

【参考方案9】:

我有同样的问题,我已经阅读了另一个主题的答案,然后我解决了另一种类似的方法。我不知道哪个更有效。 similar issue

//play button

@IBAction func startIt(sender: AnyObject) 
    startThrough();
;

//play button

func startThrough() 
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true);

    let pauseButton = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "pauseIt");
    self.toolBarIt.items?.removeLast();
    self.toolBarIt.items?.append( pauseButton );


func pauseIt() 
    timer.invalidate();

    let play = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "startThrough");
    self.toolBarIt.items?.removeLast();
    self.toolBarIt.items?.append( play );

【讨论】:

【参考方案10】:

这是苹果的疯狂之处。当你说 self.navigationItem.rightBarButtonItem.title 然后它会说 nil 而在 GUI 上它显示编辑或保存。像我这样的新人将需要大量时间来调试此行为。

要求项目将在首次加载时显示编辑,然后用户点击它它将更改为保存标题。为了存档这个,我做了如下。

//view did load 会说 Edit title

private func loadRightBarItem() 
    let logoutBarButtonItem = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(handleEditBtn))
    self.navigationItem.rightBarButtonItem  = logoutBarButtonItem

//点击编辑项目将更改为保存标题

@objc private func handleEditBtn() 
    print("clicked on Edit btn")
    let logoutBarButtonItem = UIBarButtonItem(title: "Save", style: .done, target: self, action: #selector(handleSaveBtn))
    self.navigationItem.rightBarButtonItem  = logoutBarButtonItem
    blockEditTable(isBlock: false)

//点击保存项目会显示编辑标题

@objc private func handleSaveBtn()
    print("clicked on Save btn")
    let logoutBarButtonItem = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(handleEditBtn))
    self.navigationItem.rightBarButtonItem  = logoutBarButtonItem

    saveInvitation()
    blockEditTable(isBlock: true)


【讨论】:

【参考方案11】:
func viewDidLoad()
let homeBtn: UIButton = UIButton(type: UIButtonType.custom)

        homeBtn.setImage(UIImage(named: "Home.png"), for: [])

        homeBtn.addTarget(self, action: #selector(homeAction), for: UIControlEvents.touchUpInside)

        homeBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)

        let homeButton = UIBarButtonItem(customView: homeBtn)


        let backBtn: UIButton = UIButton(type: UIButtonType.custom)

        backBtn.setImage(UIImage(named: "back.png"), for: [])

        backBtn.addTarget(self, action: #selector(backAction), for: UIControlEvents.touchUpInside)

        backBtn.frame = CGRect(x: -10, y: 0, width: 30, height: 30)

        let backButton = UIBarButtonItem(customView: backBtn)
        self.navigationItem.setLeftBarButtonItems([backButton,homeButton], animated: true)



【讨论】:

【参考方案12】:

除了上述之外,您还可以在 ios14 及以上版本中使用以下内容

   if #available(iOS 14.0, *) 
        let closeAction = UIAction(handler:  [weak self] _ in
           //perform action here
        )
        let closeBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: closeAction, menu: nil)
        navigationItem.rightBarButtonItem  = closeBarButtonItem
    

【讨论】:

以上是关于导航栏中的 UIBarButtonItem 以编程方式?的主要内容,如果未能解决你的问题,请参考以下文章

在导航栏中自定义 UIBarButtonItem

UIBarButtonItem插入导航栏时如何改变边框?

导航栏中的自定义文本

以编程方式添加到导航栏的UIBarButtonItem无法调用选择器,而添加到工具栏的按钮确实如此

无法以编程方式在我的导航栏中获取 UISearchController

导航栏中的 Barbutton 项目左栏按钮粘在左上角