如何设置按钮背景图片大小
How to set button background image size
我想在我的应用程序中创建 this 按钮。我在情节提要中的视图中添加了一个按钮。将其宽度和高度限制设置为 60px。
然后在代码中我添加了圆角半径和 UIImage 到背景,但图像填充了整个按钮背景。图片大小为 22x22px。我希望它像我的屏幕截图一样以按钮为中心。
var pencilImage = UIImage(named: "pencil")!
pencilBtn.setBackgroundImage(pencilImage, forState: UIControlState.Normal)
pencilBtn.layer.cornerRadius = pencilBtn.frame.size.height / 2
pencilBtn.clipsToBounds = true
我应该编写什么代码来解决这个问题?
使用.setImage()
方法设置图像像这样:
pencilBtn.layer.cornerRadius = pencilBtn.frame.size.height / 2
pencilBtn.clipsToBounds = true
pencilBtn.setImage(UIImage("pencil"), forState: .Normal)
pencilBtn.contentMode = UIViewContentMode.Center
我想在我的应用程序中创建 this 按钮。我在情节提要中的视图中添加了一个按钮。将其宽度和高度限制设置为 60px。
然后在代码中我添加了圆角半径和 UIImage 到背景,但图像填充了整个按钮背景。图片大小为 22x22px。我希望它像我的屏幕截图一样以按钮为中心。
var pencilImage = UIImage(named: "pencil")!
pencilBtn.setBackgroundImage(pencilImage, forState: UIControlState.Normal)
pencilBtn.layer.cornerRadius = pencilBtn.frame.size.height / 2
pencilBtn.clipsToBounds = true
我应该编写什么代码来解决这个问题?
使用.setImage()
方法设置图像像这样:
pencilBtn.layer.cornerRadius = pencilBtn.frame.size.height / 2
pencilBtn.clipsToBounds = true
pencilBtn.setImage(UIImage("pencil"), forState: .Normal)
pencilBtn.contentMode = UIViewContentMode.Center