Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift-如何在按钮上显示图像?_Swift - Fatal编程技术网

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谢谢,它确实有效。。。