ios - 1 :1 width:height constraint to UIButton programmatically

标签 ios swift autolayout aspect-ratio

<分区>

如何以编程方式快速将 UIButton 上的宽高比约束设置为 1:1 的宽高比? 此图像在 Interface Builder 中显示了我想通过代码实现的目标。

My requirements in Interface Builder

最佳答案

试试这个

myButton.addConstraint(NSLayoutConstraint(item: myButton, attribute: .height, relatedBy: .equal, toItem: myButton, attribute: .width, multiplier: 1, constant: 0))

关于ios - 1 :1 width:height constraint to UIButton programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49279316/

相关文章:

ios - 第二次按下按钮时 3D 对象的放置不起作用

ios - UITableView WillDisplayCell 方法在特定时间后调用?

Swift 中的 C# PathGeometry 等价物

ios - 容器 View Controller 中的自定义 topLayoutGuide 长度

ios - 如何使用相同的 Storyboard文件加载适用于 iPhone 型号的图像?

iOS 由于允许权限的信号 9 而终止

ios - UITableview 根据所选单元格显示不同的数据

ios - 在函数中初始化之前使用的变量

swift - "Session Failed - probably due to lack of camera access"Xcode错误

ios - 约束不适用于 UICollectionView