在导航控制器中调整自定义 UIBarButtonItem 的大小
Resize custom UIBarButtonItem in navigation controller
我正在尝试在我的应用程序导航中添加自定义 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 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
我正在尝试在我的应用程序导航中添加自定义 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 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