Swift-如何在按钮上显示图像?
我试图在一个按钮上显示一个图像,我在按钮上得到图像的蓝图,到目前为止我已经试过了-Swift-如何在按钮上显示图像?,swift,Swift,我试图在一个按钮上显示一个图像,我在按钮上得到图像的蓝图,到目前为止我已经试过了- override func viewDidLoad() { super.viewDidLoad() var birdTexture1 = UIImage(named: "fish.png") as UIImage button1.frame = CGRectMake(100, 10, 50, 150) button1.setImage(birdTexture1, forState
override func viewDidLoad()
{
super.viewDidLoad()
var birdTexture1 = UIImage(named: "fish.png") as UIImage
button1.frame = CGRectMake(100, 10, 50, 150)
button1.setImage(birdTexture1, forState: .Normal)
button1.setTitle("Testing", forState: UIControlState.Normal)
button1.addTarget(self, action: "btnAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button1)
// Do any additional setup after loading the view, typically from a nib.
}
通过上面的代码,我得到以下输出
但实际情况是这样的-
我认为您的UIButton应该是UIButtonTypeCustom类型,而不是UIButtonTypeSystem类型。您可以这样构造它,或者需要在interface builder中更新UIButton的属性
更多信息::我想替换此代码:
let birdTexture1 = UIImage(named: "fish.png")! as UIImage
let button1:UIButton=UIButton(frame:CGRectMake(100, 50, 50, 150))
button1.setBackgroundImage(birdTexture1, forState: .Normal)
button1.setTitle("Testing", forState: .Normal)
button1.addTarget(self, action: #selector(ViewController.btnActionClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button1)
@IBAction func btnActionClick(sender:AnyObject){
}
还可以添加到目标中的图像将按钮类型更改为
uibuttonypecustom
@0x7fffffff谢谢,它确实有效。。。