【问题标题】:Resize custom UIBarButtonItem in navigation controller在导航控制器中调整自定义 UIBarButtonItem 的大小
【发布时间】:2019-06-13 12:24:38
【问题描述】:

我正在尝试在我的应用导航中添加自定义 UIBarButtonItem。 使用以下代码,我设法添加了一个

let rightButton = UIButton(type: .custom)
    rightButton.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
    rightButton.setImage(UIImage(named: "hamburgerMenuIcon"), for: .normal)
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)                  target: nil, action: nil)

但看起来 rightButton.frame 不起作用,因为它根本不会改变按钮的尺寸。

【问题讨论】:

    标签: ios swift uibutton uibarbuttonitem


    【解决方案1】:

    从 iOS 11 导航栏出现自动布局,因此框架设置可能不起作用。使用以下代码

    let rightButton = UIButton(type: .custom)
    rightButton.frame = CGRect(x: 0.0, y: 0.0, width: 10, height: 10)
    rightButton.setImage(UIImage(named:"hamburgerMenuIcon"), for: .normal)
    
    let menuBarItem = UIBarButtonItem(customView: rightButton)
    let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 10)
    currWidth?.isActive = true
    let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 10)
    currHeight?.isActive = true
    navigationItem.rightBarButtonItem = menuBarItem
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多