ios 自定义 UIButton 缩放 setImage 向下不起作用

标签 ios swift uibutton

如果我以编程方式添加 UIButton,则在缩小 setImage 大小时遇到​​问题,而如果我通过 Storyboard添加 UIButton,则 UIButton 大小保持正确。 图片是128x128,我想缩小到30x30

enter image description here 顶部一个是使用 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 上的图像 enter image description here

最佳答案

这个怎么样:

checkBox.frame.size.width = 30
checkBox.frame.size.height = 30

关于ios 自定义 UIButton 缩放 setImage 向下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942303/

相关文章:

objective-c - 将桥接头添加到现有目标项目时出错

ios - 如何为此 View 设置约束?

ios - CGRect 调用中的 Swift 额外参数 "width"

ios - 如何在以编程方式创建的按钮上添加 Spinner Indicator

ios - 无法从 Apple APN 生产服务器获得响应

objective-c - 无法从 iPhone 播放本地视频

ios - 无法执行 segue 退出

ios - 获取 MPMusicPlayer 的当前位置

iOS 音频单元获取当前时间戳

swift - 如何快速调用电话