我一直在尝试构建自定义 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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |