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: .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.您要么构造它,要么需要在界面构建器中更新UIButton的属性.

更多信息:https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/clm/UIButton/buttonWithType

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...