以编程方式创buildbutton并设置背景图像

当我尝试在Swift中创buildbutton并设置背景图像时:

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) 

我总是得到一个错误:“ 无法转换expression式的types'Void'键入'UIImage' ”。

你的代码应该如下所示

 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) 

试试下面:

 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) 

让我知道它是否有效?

您需要检查图像不是零,然后将其分配给button。

例:

 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) 

亚历克斯·雷诺斯答案的SWIFT 3版本

 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) 

用于UIButton的便捷初始化器已经改变为swift 2.0(iOS9,Xcode 7)

 convenience init(type buttonType: UIButtonType) 

创buildbutton,并添加图像作为其背景swift 3.0(iOS10,Xcode 8.3)。

  let image = UIImage(named: "imgname") as UIImage? let button = UIButton(type: UIButtonType.system) button.frame = CGRect.init(x: 10, y: 10, width: 100, height: 45) button.setImage(image, for: .normal) self.view.addSubview(button) 
 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() 

这是如何创build一个美丽的button与边框和圆形的边缘:

 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) 

Swift:Uibutton以编程方式创build

 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)