在Swift 3中更改UIBarButtonItem(image)的大小

时间:2017-03-28 15:29:18

标签: ios xcode swift3

我正在尝试更改导航栏中某些图标的大小,但我对如何执行此操作感到有点困惑?到目前为止我的代码是:

func setUpNavBarButtons() {
    let moreButton = UIBarButtonItem (image: UIImage(named:"ic_more_vert_3")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleMore))
    navigationItem.rightBarButtonItems = [moreButton]
    let refreshButton = UIBarButtonItem (image: UIImage(named:"ic_refresh")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(refreshDataButton))
    navigationItem.leftBarButtonItems = [refreshButton]
}

任何帮助?

7 个答案:

答案 0 :(得分:63)

我就这样做了

iOS 10及以下

func setUpMenuButton(){
    let menuBtn = UIButton(type: .custom)
    menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 20, height: 20)
    menuBtn.setImage(UIImage(named:"menuIcon"), for: .normal)
    menuBtn.addTarget(self, action: #selector(vc.onMenuButtonPressed(_:)), for: UIControlEvents.touchUpInside)

    let menuBarItem = UIBarButtonItem(customView: menuBtn)
    self.navigationItem.leftBarButtonItem = menuBarItem
}

iOS 11 - 导航栏出现了Autolayout,因此框架设置可能无法正常工作

func setUpMenuButton(){
    let menuBtn = UIButton(type: .custom)
    menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 20, height: 20)
    menuBtn.setImage(UIImage(named:"menuIcon"), for: .normal)
    menuBtn.addTarget(self, action: #selector(vc.onMenuButtonPressed(_:)), for: UIControlEvents.touchUpInside)

    let menuBarItem = UIBarButtonItem(customView: menuBtn)
    let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 24)
    currWidth?.isActive = true
    let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 24)
    currHeight?.isActive = true
    self.navigationItem.leftBarButtonItem = menuBarItem
}

答案 1 :(得分:10)

Swift 4.2的扩展

另一种实现方式,是 anoop4real

提供的答案

但是使用按钮类型.system可以将全局色应用于您的图标

用法:

navigationItem.leftBarButtonItem = UIBarButtonItem.menuButton(self, action: #selector(presentSettings), imageName: "settings")

实施:

extension UIBarButtonItem {

    static func menuButton(_ target: Any?, action: Selector, imageName: String) -> UIBarButtonItem {
        let button = UIButton(type: .system)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.addTarget(target, action: action, for: .touchUpInside)

        let menuBarItem = UIBarButtonItem(customView: button)
        menuBarItem.customView?.translatesAutoresizingMaskIntoConstraints = false
        menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 24).isActive = true
        menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 24).isActive = true

        return menuBarItem
    }
}

答案 2 :(得分:4)

您可以按如下方式配置按钮的框架:

let icon = UIImage(named: "imageName")
let iconSize = CGRect(origin: CGPoint.zero, size: CGSize(width: 50, height: 50))
let iconButton = UIButton(frame: iconSize)
iconButton.setBackgroundImage(icon, for: .normal)
let barButton = UIBarButtonItem(customView: iconButton)
iconButton.addTarget(self, action: #selector(foo), for: .touchUpInside)

答案 3 :(得分:1)

最后,我这样做了,并且有效:

let moreButton = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 35))
moreButton.setBackgroundImage(UIImage(named: "ic_more_vert_3"), for: .normal)
moreButton.addTarget(self, action: #selector(TableViewController.handleMore), for: .touchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: moreButton)

回答:Change width of a UIBarButtonItem in a UINavigationBar in swift

答案 4 :(得分:0)

您可以使用此功能配置条形按钮:

public convenience init(customView: UIView)

您可以根据需要初始化自定义视图。 之后,您可以根据需要通过UIBarButtonItem&#39>访问该视图:

open var customView: UIView?

提示:由于UIButtonUIView的子类,因此您也可以直接使用它。

答案 5 :(得分:0)

@DogCoffee答案是解决问题的一种很好的创新方法。我可以建议一些稍微修改,以考虑大小和tintColor

extension UIBarButtonItem {

    static func menuButton(_ target: Any?,
                           action: Selector,
                           imageName: String,
                           size:CGSize = CGSize(width: 32, height: 32),
                           tintColor:UIColor?) -> UIBarButtonItem
    {
        let button = UIButton(type: .system)
        button.tintColor = tintColor
        button.setImage(UIImage(named: imageName), for: .normal)
        button.addTarget(target, action: action, for: .touchUpInside)

        let menuBarItem = UIBarButtonItem(customView: button)
        menuBarItem.customView?.translatesAutoresizingMaskIntoConstraints = false
        menuBarItem.customView?.heightAnchor.constraint(equalToConstant: size.height).isActive = true
        menuBarItem.customView?.widthAnchor.constraint(equalToConstant: size.width).isActive = true

        return menuBarItem
    }
}

答案 6 :(得分:0)

尝试使用 pod 'MTSDK',在代码中使用 swift 非常方便。

let swapButton = UIButton()
swapButton.setImage(UIImage(named: "ic_swap"), for: .normal)
swapButton.handle {
 self.swapMode()
}
    
let rightButton = UIBarButtonItem(customView: swapButton)
rightButton.customView?.translatesAutoresizingMaskIntoConstraints = false
rightButton.customView?.snp.makeConstraints {
 $0.width.height.equalTo(32)
}
navigationItem.rightBarButtonItem = rightButton