Swift:图像大于按钮的按钮

时间:2018-04-25 20:26:42

标签: swift uibutton

我无法找到一种方法,使按钮看起来如下图所示。

image of the button

图标应该比按钮大。

我已经厌倦了,但它不起作用:

let image = UIImage(named: "CircleForButtonCountries")
goToCoutriesButtonMain.imageView?.contentMode = UIViewContentMode.scaleAspectFit
goToCoutriesButtonMain.setImage(image, for: UIControlState.normal)

2 个答案:

答案 0 :(得分:0)

试试此代码

let image = UIImage(named: "CircleForButtonCountries")
goToCoutriesButtonMain.imageView?.contentMode = 
UIViewContentMode.scaleAspectFit
goToCoutriesButtonMain.setBackgroundImage(image, for: UIControlState.normal)  // use setBackgroundImage instead of setImage 

答案 1 :(得分:0)

在Swift 5中工作:

yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)