如果我以编程方式添加 UIButton,则在缩小 setImage 大小时遇到问题,而如果我通过 Storyboard添加 UIButton,则 UIButton 大小保持正确。 图片是128x128,我想缩小到30x30
顶部一个是使用 Storyboard 创建的,底部是通过编程方式创建的
这些是我尝试过的方法,没有一个能够缩小图像......
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.frame.size.width = 30
checkBox.frame.size.height = 30
关于ios 自定义 UIButton 缩放 setImage 向下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942303/