How do you create a UIImage View Programmatically - Swift

257,002

Solution 1

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!)

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)

Solution 2

First create UIImageView then add image in UIImageView .

    var imageView : UIImageView
    imageView  = UIImageView(frame:CGRectMake(10, 50, 100, 300));
    imageView.image = UIImage(named:"image.jpg")
    self.view.addSubview(imageView)

Solution 3

This answer is update to 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
    }

}

Solution 4

You can use above in one line.

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

Solution 5

In 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)
Share:
257,002
alex
Author by

alex

I'm 14 years old.

Updated on December 14, 2020

Comments

  • alex
    alex over 3 years

    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))
    

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

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

  • Alec O
    Alec O almost 7 years
    You can, but it's not exactly "clean" Swift
  • MartianMartian
    MartianMartian over 6 years
    size and scale?
  • kakubei
    kakubei over 5 years
    Undervoted for too many lines of code. Would be better to write it like this: let imageView = UIImageView(named: "Test.jpeg"); self.view.addSubview(imageView). 2 lines and you use a constant instead of a variable. Then use Autoconstraint to place it.
  • midtownguru
    midtownguru over 4 years
    In swift 4.2 "unexpectedly found nil when unwrapping optional" error was thrown from the second line.
  • midtownguru
    midtownguru over 4 years
    This answer works for Swift 4.2. For somebody like a novice, extension might add confusion to the understanding of the code even though it's still nice to have.