ios - Swift - UIButton 以编程方式设置约束
问题描述
我在设置我的height
和width
constraints
为我的UIButton
. 我不知道为什么这段代码不起作用:
let wishButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(named: "wishButton"), for: .normal)
v.translatesAutoresizingMaskIntoConstraints = false
v.addTarget(self, action: #selector(wishButtonTapped), for: .touchUpInside)
return v
}()
这些是我的限制:
wishButton.centerXAnchor.constraint(equalTo: popUpView.centerXAnchor).isActive = true
wishButton.centerYAnchor.constraint(equalTo: popUpView.centerYAnchor, constant: 150).isActive = true
wishButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
wishButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
这是它目前的样子:
这很奇怪,因为其他两个constraints
工作正常。可能只是一个愚蠢的错误,但我很新,并感谢每一个帮助:)
解决方案
我注意到的一件事是您似乎没有设置按钮的框架。
初始化按钮后尝试添加此行:
v.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
我不确定是否需要,但如果您的按钮根本没有显示,这可能会解决问题
推荐阅读
- excel - 在单元格值分配中包括以 3 分钟为间隔的时间
- javascript - 使用 Vue 更改数据时添加背景颜色
- domain-driven-design - 在控制器或域模型中放置基本验证的位置
- php - 如何使用 preg-match 纠正 HTTPS out URL 错误?
- selenium - 我收到了 StaleElementReferenceException。在以下情况下我该如何解决这个问题
- javascript - 10秒后杀死一个子进程 - NodeJS
- vue.js - 为什么我在 nuxt.config.js 中的全局样式和 js 文件不起作用?
- c - 如何通过将字符串与指针启动进行比较来从“是,否”问题中进行条件循环?[C编程]
- visual-studio-2019 - 如何在 Visual Studio 2019 中使用 editorconfig?
- android - 不能使用改造的内联功能