如何以编程方式创建UIImage视图 - Swift [英] How do you create a UIImage View Programmatically - Swift

查看:158
本文介绍了如何以编程方式创建UIImage视图 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式创建UIImage视图,我有一个新视图,我尝试这样做

I'm trying to create a UIImage View programmatically, I have a new view and I tried doing this

let imageName = "yourImage.png"
yourview.backgroundColor = UIColor.colorWithPatternImage(UIImage(named:imageName))

这不起作用,因为我不知道第二行应该是 yourview

this did not work because I don't know what this should be yourview in the second line.

问题:
如何在屏幕上显示UIImageView而不是在故事板中进行编码

Question: How do I make a UIImageView appear on the screen by coding it instead of doing it in the storyboard

推荐答案

首先从图像文件中创建 UIImage ,然后从中创建 UIImageView

First you create a UIImage from your image file, then create a UIImageView from that:

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)

最后你需要给 imageView 一个框架并添加你的视图以使其可见:

Finally you'll need to give imageView a frame and add it your view for it to be visible:

imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(imageView)

这篇关于如何以编程方式创建UIImage视图 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆