【发布时间】:2016-10-09 10:03:31
【问题描述】:
如果我以编程方式添加 UIButton,我在缩小 setImage 大小时遇到问题,而如果我通过情节提要添加 UIButton,则 UIButton 大小保持正确。 图片是 128x128,我想缩小到 30x30
这些都是我试过的方法,都不能缩小图片...
let checkBox = UIButton(type .system)
checkBox.sizeThatFits(CGSize(width: 30, height: 30))
checkBox.contentMode = .scaleAspectFit
checkBox.contentVerticalAlignment = .fill
checkBox.contentHorizontalAlignment = .fill
checkBox.heightAnchor.constraint(equalToConstant: 15.0)
checkBox.widthAnchor.constraint(equalToConstant: 15.0)
图像在运行时设置/更新 checkBox.setImage(UImage, for: .normal)
建议使用checkBox.frame.size.width, checkBox.frame.size.height 图像大小正确,但 UIButton 现在有额外空间。 就像下图一样。我在 touchUP UIButton 上更新图像
【问题讨论】:
-
我设法解决了它。 checkBox.heightAnchor.constraint(equalToConstant: 15.0) checkBox.widthAnchor.constraint(equalToConstant: 15.0) 是正确的方向,除了我必须将它设置为 isActive = true ......这是一个愚蠢的想法,将它也设置为额外的代码来激活.. .