【问题标题】:Change size of UIBarButtonItem (image) in Swift 3在 Swift 3 中更改 UIBarButtonItem(图像)的大小
【发布时间】:2017-08-21 18:34:32
【问题描述】:

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

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]
}

有什么帮助吗?

【问题讨论】:

    标签: ios xcode swift3


    【解决方案1】:

    我就是这样做的

    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 - 导航栏带有自动布局,因此框架设置可能不起作用

    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
    }
    

    【讨论】:

    • 这应该被标记为正确答案,它是唯一处理 iOS 11 的答案。
    • 是的,约束方法很适合我,iOS 11.3,Xcode Version 9.3 (9E145),Swift 4.1
    • 我不知道导航栏在 iOS 11 中有 Autolayout。谢谢伙计。
    【解决方案2】:

    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
        }
    }
    

    【讨论】:

    • 谢谢。由于某种原因,最佳答案中的方法不像往常那样对我有用,但这很好用。
    • 您应该将其添加到您的代码中,以确保图像在不是完美正方形时正确显示:button.imageView?.contentMode = .scaleAspectFit。顺便说一句,您的答案是唯一一个在 iOS 14.4 上使用 Swift 5 对我有用的答案。谢谢。
    • 在 iOS 14.5 中为我工作。谢谢兄弟,这应该是最新iOS版本的答案。
    【解决方案3】:

    您可以像下面这样配置按钮的框架:

    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
    

    【讨论】:

    • 您好,我已尝试实现您的代码,但出现以下错误:无法将“Int”类型的值转换为预期的参数类型“CGSize”。什么是尺寸配置。它只是大小:50 等
    • @Sole 是一个有宽高的结构体
    • 这可行,但您无法更改按钮的色调
    • @FedeHenze 您需要像这样UIImage(named: "imageName")?.withRenderingMode(.alwaysTemplate) 创建icon。然后你就可以设置它的色调了。查看this 了解更多信息。
    【解决方案4】:

    最后我这样做了,它成功了:

    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

    【讨论】:

    • 为我工作,iOS 13.3.1
    【解决方案5】:

    @DogCoffee 回答是解决问题的好方法。我可以建议一些稍微修改一下以考虑尺寸和色调

    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】:

      如果您使用的是 iOS 13 提供的 SF 符号图像,您可以设置 SF 符号图像配置。

      let config = UIImage.SymbolConfiguration(pointSize: 30, weight: .light, scale: .default)
      
      let image = UIImage(systemName: "plus.circle", withConfiguration: config)
      
      

      【讨论】:

        【解决方案7】:

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

        public convenience init(customView: UIView)
        

        您可以根据需要初始化自定义视图。 之后,如果需要,您可以通过UIBarButtonItem's 访问该视图:

        open var customView: UIView?
        

        提示:因为UIButtonUIView的子类,你也可以直接使用。

        【讨论】:

        • 而不是使用let moreButton = UIBarButtonItem (image: UIImage(named:"ic_more_vert_3")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleMore)) 创建一个 UIButton 并在其上设置“ic_more_vert_3”图像。比使用let moreButton = init(customView: theButtonThatYouCreated)
        【解决方案8】:

        尝试使用 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-18
          相关资源
          最近更新 更多