快速更改 UINavigationBar 中 UIBarButtonItem 的宽度

Posted

技术标签:

【中文标题】快速更改 UINavigationBar 中 UIBarButtonItem 的宽度【英文标题】:Change width of a UIBarButtonItem in a UINavigationBar in swift 【发布时间】:2016-01-20 05:06:17 【问题描述】:

我需要在 swift 2.0 中为我的导航栏按钮设置框架

我试过这个代码

self.navigationController!.navigationBar.drawRect(CGRectMake(0, 0, 30, 30)) 

但它不会起作用

提前致谢

【问题讨论】:

你是如何添加按钮的? 我想设置按钮只点击框架,但是当我点击按钮外部时它会调用该方法 【参考方案1】:
// Swift 3
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
backButton.setBackgroundImage(UIImage(named: "img"), for: .normal)
backButton.addTarget(self, action: "action:", for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)

// Swift 2
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
backButton.setBackgroundImage(UIImage(named: "img"), forState: .Normal)
backButton.addTarget(self, action: "action:", forControlEvents: .TouchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)

【讨论】:

【参考方案2】:
    // We dont have  Property to change UIBarButtonItem frame
    // So we can creat UIButton() and give requered frame and add to navigationItem.setLeftBarButtonItems
    // Please refere Belove code

    // Swift 2.0

    let btnBack   = UIButton()
    btnBack.frame = CGRectMake(0, 0, 100, 64)
    btnBack.addTarget(self, action: "backAction", forControlEvents: UIControlEvents.TouchUpInside)
    let leftBarBackBtn: UIBarButtonItem = UIBarButtonItem(customView: btnBack)
    self.navigationItem.setLeftBarButtonItems([ leftBarBackBtn ], animated: false)

    // Please submit your answer with Explanation comments to improve your Quality or Answer and question

【讨论】:

【参考方案3】:

swift 3 的最佳答案:

 let homeButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
 homeButton.setBackgroundImage(#imageLiteral(resourceName: "home-1"), for: .normal)
 homeButton.addTarget(self, action: #selector(homePressed), for: .touchUpInside)
 self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: homeButton)

【讨论】:

【参考方案4】:
    func createFakeAndSearchCurrentLocationBarButton (vw: UIViewController) 

    let fakeCurrentLocationGo = UIButton(type: .custom)
    fakeCurrentLocationGo.setImage(UIImage(named: "reallocationgo50"), for: .normal)
    fakeCurrentLocationGo.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    fakeCurrentLocationGo.addTarget(vw, action: #selector(goToMyCurrentLocationPin), for: .touchUpInside)
    let leftItem = UIBarButtonItem(customView: fakeCurrentLocationGo)


    let searchLocationBtn = UIButton(type: .custom)
    searchLocationBtn.setImage(UIImage(named: "search"), for: .normal)
    searchLocationBtn.frame = CGRect(x: 0, y: 0, width: 15, height: 15)
    fakeCurrentLocationGo.addTarget(vw, action: #selector(searchLocationHandle), for: .touchUpInside)
    let rightItem = UIBarButtonItem(customView: searchLocationBtn)

    vw.navigationItem.setRightBarButtonItems([leftItem,rightItem], animated: true)


【讨论】:

请更新以包含对此代码块的解释。

以上是关于快速更改 UINavigationBar 中 UIBarButtonItem 的宽度的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式快速更改导航栏的高度

更改 UINavigationBar 字体属性?

旋转更改后 UINavigationBar 消失

UINavigationBar 在推送时更改颜色

UINavigationBar 无法更改颜色

在 Swift 中更改 UINavigationBar 字体