ios 如何以编程方式创建 UIImage 视图 - Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26569371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you create a UIImage View Programmatically - Swift
提问by alex
I'm trying to create a UIImage View programmatically, I have a new view and I tried doing this
我正在尝试以编程方式创建 UIImage 视图,我有一个新视图,我尝试这样做
let imageName = "yourImage.png"
yourview.backgroundColor = UIColor.colorWithPatternImage(UIImage(named:imageName))
this did not work because I don't know what this should be yourviewin the second line.
这不起作用,因为我不知道你在第二行中的观点应该是什么。
Question:How do I make a UIImageView appear on the screen by coding it instead of doing it in the storyboard
问题:如何通过编码而不是在情节提要中使 UIImageView 出现在屏幕上
回答by Nate Cook
First you create a UIImage
from your image file, then create a UIImageView
from that:
首先UIImage
从您的图像文件创建一个,然后UIImageView
从该文件创建一个:
let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
Finally you'll need to give imageView
a frame and add it your view for it to be visible:
最后,您需要提供imageView
一个框架并将其添加到您的视图中以使其可见:
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(imageView)
回答by Shanmugasundharam
First create UIImageView then add image in UIImageView .
首先创建 UIImageView 然后在 UIImageView 中添加图像。
var imageView : UIImageView
imageView = UIImageView(frame:CGRectMake(10, 50, 100, 300));
imageView.image = UIImage(named:"image.jpg")
self.view.addSubview(imageView)
回答by MEnnabah
This answer is update to Swift 3.
这个答案是对 Swift 3 的更新。
This is how you can add an image view programmatically where you can control the constraints.
这是您可以通过编程方式添加图像视图的方式,您可以在其中控制约束。
Class ViewController: UIViewController {
let someImageView: UIImageView = {
let theImageView = UIImageView()
theImageView.image = UIImage(named: "yourImage.png")
theImageView.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
return theImageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(someImageView) //This add it the view controller without constraints
someImageViewConstraints() //This function is outside the viewDidLoad function that controls the constraints
}
// do not forget the `.isActive = true` after every constraint
func someImageViewConstraints() {
someImageView.widthAnchor.constraint(equalToConstant: 180).isActive = true
someImageView.heightAnchor.constraint(equalToConstant: 180).isActive = true
someImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
someImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 28).isActive = true
}
}
回答by Vikram Pote
You can use above in one line.
您可以在一行中使用上面的内容。
let imageView = UIImageView(image: UIImage(named: "yourImage.png")!)
回答by Sandeep Singh
In Swift 3.0 :
在 Swift 3.0 中:
var imageView : UIImageView
imageView = UIImageView(frame:CGRect(x:10, y:50, width:100, height:300));
imageView.image = UIImage(named:"Test.jpeg")
self.view.addSubview(imageView)
回答by Fullpower
Thanks, MEnnabah, just to add to your code where you are missing the =
sign in the declaration statement:
谢谢,Mennabah,只是为了添加到您的代码中您缺少=
声明语句中的符号:
let someImageView: UIImageView = {
let theImageView = UIImageView()
theImageView.image = UIImage(named: "yourImage.png")
theImageView.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
return theImageView
}()
Everything else is, all perfect for Swift 3.
其他一切都非常适合 Swift 3。
回答by iOS
In Swift 4.2 and Xcode 10.1
在 Swift 4.2 和 Xcode 10.1 中
//Create image view simply like this.
let imgView = UIImageView()
imgView.frame = CGRect(x: 200, y: 200, width: 200, height: 200)
imgView.image = UIImage(named: "yourimagename")//Assign image to ImageView
imgView.imgViewCorners()
view.addSubview(imgView)//Add image to our view
//Add image view properties like this(This is one of the way to add properties).
extension UIImageView {
//If you want only round corners
func imgViewCorners() {
layer.cornerRadius = 10
layer.borderWidth = 1.0
layer.masksToBounds = true
}
}
回答by Joel
Swift 4:
斯威夫特 4:
First create an outlet for your UIImageView
首先为您的 UIImageView 创建一个插座
@IBOutlet var infoImage: UIImageView!
Then use the image property in UIImageView
然后使用 UIImageView 中的 image 属性
infoImage.image = UIImage(named: "icons8-info-white")