加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift-如何在按钮上显示图像?

发布时间:2020-12-14 05:38:40 所属栏目:百科 来源:网络整理
导读:我正试图在一个按钮上显示一个图像,我正在通过按钮获得图像的蓝色打印,我到目前为止已尝试过 – override func viewDidLoad(){ super.viewDidLoad() var birdTexture1 = UIImage(named: "fish.png") as UIImage button1.frame = CGRectMake(100,10,50,150) b
我正试图在一个按钮上显示一个图像,我正在通过按钮获得图像的蓝色打印,我到目前为止已尝试过 –
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:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读