Change size of UIBarButtonItem (image) in Swift 3

前端 未结 6 2043
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 07:51

I am trying to change the size of some icons in my navBar, but I am a little confused as to how to do this? My code so far is:

func setUpNavBarButtons() {
          


        
相关标签:
6条回答
  • 2020-12-14 08:26

    @DogCoffee answer is a great and creative way to solve the problem. May I suggest some slightly mods in order to take into account size and 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
        }
    }
    
    0 讨论(0)
  • 2020-12-14 08:28

    You can configure the frame of you button like below:

    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)
    
    navigationItem.leftBarButtonItem = barButton
    
    0 讨论(0)
  • 2020-12-14 08:29

    Extension for Swift 4.2

    Just a different way of implementation the answer provided by anoop4real

    However using button type .system allows the global tint to be applied to your icon

    Usage:

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

    Implementation:

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-14 08:35

    This is how I did it

    iOS 10 and below

    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 - Navigation bar come up with Autolayout so frame setting may not work

    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
    }
    
    0 讨论(0)
  • 2020-12-14 08:46

    In the end I did it like this and it worked:

    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)
    

    Answer from: Change width of a UIBarButtonItem in a UINavigationBar in swift

    0 讨论(0)
  • 2020-12-14 08:46

    You can configure the bar buttons using this function:

    public convenience init(customView: UIView)
    

    And You can init the custom view as You desire. After that You can access the view if needed via UIBarButtonItem's:

    open var customView: UIView?
    

    Hint: Because UIButton is a child class of UIView, You can directly use it too.

    0 讨论(0)
提交回复
热议问题