我正在尝试在应用程序的导航中添加自定义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不能正常工作,因为它根本不会改变按钮的尺寸。
答案 0 :(得分:0)
在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