我一直在尝试构建自定义 UINavigationBar,但我的 button.frame 输入没有任何效果。
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "I_sent"), for: .normal)
button.addTarget(self, action: #selector(action), for: .touchUpInside)
// This doesn't seem to have an effect:
button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
button.contentMode = .scaleAspectFit
let item = UIBarButtonItem(customView: button)
navigationItem.rightBarButtonItem = item
}
@objc func action() {
print("Action")
}
代码(上面)运行时如下所示:
但我试图让它看起来像这样:
非常感谢任何帮助!
最佳答案
向 UIButton
添加约束:
button.widthAnchor.constraint(equalToConstant: button.frame.size.width).isActive = true
button.heightAnchor.constraint(equalToConstant: button.frame.size.height).isActive = true
希望对你有帮助!
关于ios - Swift 4 - 添加到 UINavigationBar 时无法控制 UIButton 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701332/