以编程方式创建按钮并设置背景图像
Posted
技术标签:
【中文标题】以编程方式创建按钮并设置背景图像【英文标题】:Create a button programmatically and set a background image 【发布时间】:2014-06-05 15:52:22 【问题描述】:当我尝试在 Swift 中创建按钮并设置背景图片时:
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setImage(IMAGE, forState: UIControlState.Normal)
button.addTarget(self, action: "btnTouched:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
我总是收到错误消息:“无法将表达式的类型 'Void' 转换为类型 'UIImage'”。
【问题讨论】:
你能显示你声明IMAGE
的代码吗?
你是如何创建图像的?可能需要打开它 IMAGE!
IMAGE
应该看起来像 UIImage(named: "name")
谢谢你的作品!我只说:“button.setImage("CircleTap", forState: UIControlState.Normal)" 但现在它完美地工作了
【参考方案1】:
您的代码应如下所示
let image = UIImage(named: "name") as UIImage?
let button = UIButton(type: UIButtonType.Custom) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside)
self.view.addSubview(button)
【讨论】:
我认为它实际上应该是as? UIImage
如果imageNamed:
返回nil,您的版本将崩溃。
好吧,如果它是 nil,那是你的错,但你可以添加 ?但是当setImage
时,您还必须注意这一点。我想这取决于您想要获得的防御程度,并且由于您控制本地资产,因此一两个单元测试应该不足为奇
圆圈图片呢?
尝试添加 ? as UIImage?
如果您不想看到蓝色按钮 button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton,请将按钮类型更改为自定义【参考方案2】:
试试下面:
let image = UIImage(named: "ImageName.png") as UIImage
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button .setBackgroundImage(image, forState: UIControlState.Normal)
button.addTarget(self, action: "Action:", forControlEvents:UIControlEvents.TouchUpInside)
menuView.addSubview(button)
让我知道它是否有效?
【讨论】:
您应该将 buttonWithType 设置为 UIButtonType.Custom,而不是 System,直到图像显示出来【参考方案3】:Swift 5 接受答案的版本:
let image = UIImage(named: "image_name")
let button = UIButton(type: UIButton.ButtonType.custom)
button.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(function), for: .touchUpInside)
//button.backgroundColor = .lightGray
self.view.addSubview(button)
当然在哪里
@objc func function() ...
图片默认居中对齐。您可以通过设置按钮的 imageEdgeInsets 来更改它,如下所示:
// In this case image is 40 wide and aligned to the left
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: button.frame.width - 45)
【讨论】:
【参考方案4】:SWIFT 3 版本 Alex Reynolds 的回答
let image = UIImage(named: "name") as UIImage?
let button = UIButton(type: UIButtonType.custom) as UIButton
button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
button.setImage(image, for: .normal)
button.addTarget(self, action: Selector("btnTouched:"), for:.touchUpInside)
self.view.addSubview(button)
【讨论】:
【参考方案5】:在将图像分配给按钮之前,您需要检查图像不为零。
例子:
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
if let image = UIImage(named: "imagename.png")
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside)
self.view.addSubview(button)
【讨论】:
如果图像不在项目中,我想我希望应用程序崩溃。那为什么要检查呢? 因为这样应用程序就不会崩溃并且可能会附带丢失的图像【参考方案6】:创建按钮并添加图像作为其背景 swift 4
let img = UIImage(named: "imgname")
let myButton = UIButton(type: UIButtonType.custom)
myButton.frame = CGRect.init(x: 10, y: 10, width: 100, height: 45)
myButton.setImage(img, for: .normal)
myButton.addTarget(self, action: #selector(self.buttonClicked(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(myButton)
【讨论】:
更正:第 4 行] button.setImage(image, for: .normal)【参考方案7】:let btnRight=UIButton.buttonWithType(UIButtonType.Custom) as UIButton
btnRight.frame=CGRectMake(0, 0, 35, 35)
btnRight.setBackgroundImage(UIImage(named: "menu.png"), forState: UIControlState.Normal)
btnRight.setTitle("Right", forState: UIControlState.Normal)
btnRight.tintColor=UIColor.blackColor()
【讨论】:
【参考方案8】:您可以通过以下方法创建一个带有边框和圆边的漂亮按钮:
loginButton = UIButton(frame: CGRectMake(self.view.bounds.origin.x + (self.view.bounds.width * 0.325), self.view.bounds.origin.y + (self.view.bounds.height * 0.8), self.view.bounds.origin.x + (self.view.bounds.width * 0.35), self.view.bounds.origin.y + (self.view.bounds.height * 0.05)))
loginButton.layer.cornerRadius = 18.0
loginButton.layer.borderWidth = 2.0
loginButton.backgroundColor = UIColor.whiteColor()
loginButton.layer.borderColor = UIColor.whiteColor().CGColor
loginButton.setTitle("Login", forState: UIControlState.Normal)
loginButton.setTitleColor(UIColor(red: 24.0/100, green: 116.0/255, blue: 205.0/205, alpha: 1.0), forState: UIControlState.Normal)
【讨论】:
【参考方案9】:设置背景我们不能再使用forState
,所以我们必须使用for
来设置UIControlState
:
let zoomInImage = UIImage(named: "Icon - plus") as UIImage?
let zoomInButton = UIButton(frame: CGRect(x: 10), y: 10, width: 45, height: 45))
zoomInButton.setBackgroundImage(zoomInImage, for: UIControlState.normal)
zoomInButton.addTarget(self, action: #selector(self.mapZoomInAction), for: .touchUpInside)
self.view.addSubview(zoomInButton)
【讨论】:
我使用此代码将缩放按钮放在谷歌地图上。使用上面的代码设置背景图像。和之前不一样了。 它与其他答案有何不同? 现在我们不能使用forState来设置背景,现在我们必须使用“for”来设置UIControlState【参考方案10】:更新到 Swift 3
let image = UIImage(named: "name")
let button = UIButton(type: .custom)
button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(self.btnAbsRetClicked(_:)), for:.touchUpInside)
self.view.addSubview(button)
【讨论】:
【参考方案11】:Swift:以编程方式创建 Ui 按钮
let myButton = UIButton()
myButton.titleLabel!.frame = CGRectMake(15, 54, 300, 500)
myButton.titleLabel!.text = "Button Label"
myButton.titleLabel!.textColor = UIColor.redColor()
myButton.titleLabel!.textAlignment = .Center
myButton.addTarget(self,action:"Action:",forControlEvents:UIControlEvent.TouchUpInside)
self.view.addSubview(myButton)
【讨论】:
以上是关于以编程方式创建按钮并设置背景图像的主要内容,如果未能解决你的问题,请参考以下文章