【发布时间】:2019-05-24 09:36:36
【问题描述】:
我刚刚在 Swift 中为 iOS 启动了一个应用程序,我正在以编程方式进行设计(我的意思是我没有使用故事板)
我有一个带有图像的 UIButton,我希望图像填充整个按钮。现在它只在我的按钮中显示图像,但比按钮小,在按钮框架的顶部居中。
我的代码是:
let buttonLocation = UIButton(frame: CGRect(x: 0, y: 0, width: buttonRoundedSize, height: buttonRoundedSize))
buttonLocation.setImage(UIImage(named: "locate_button"), for: .normal)
buttonLocation.backgroundColor = .white
buttonLocation.layer.cornerRadius = frame.width / 2
buttonLocation.myExtension()
这是我尝试过的东西(这些都不起作用):
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
self.autoresizingMask = .flexibleWidth
self.autoresizingMask = .flexibleHeight
self.imageView?.contentMode = .scaleAspectFill
如果我只是放置 contentMode 行,它也不起作用。我检查过的 imageView 没有返回 nil ..
谢谢!
【问题讨论】:
-
试试 buttonLocation.contentMode = .scaleAspectFit
-
使用
buttonLocation.setBackgroundImage(UIImage(named: "locate_button"), for: .normal) -
@BenRockey 它不起作用
-
尝试在按钮内部设置imageView对象的内容模式。前任。 buttonLocation.imageView?.contentMode = .scaleAspectFill
-
@BhargavR 也不起作用 :( 而且我也尝试过 setBackgroundImage 并且我遇到了完全相同的问题
标签: ios swift uibutton contentmode