Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (33 sloc) 1.41 KB

Adding UIImageView

create a UIImageView

// step 1: create a UIImage
let myImage = UIImage(named:"icebear")

// step 2: create a UIImageView
let myImageView = UIImageView(myImage)

// step 3: add UIImageView to UIView 
// your ViewController will have a property `view`

use autolayout

// step 1: set this manually
// when you use interface builder, this is set automatically
imageView.translatesAutoresizingMaskIntoConstraints = false

// step 2: add your contraints (remember to set active to true)
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

// step 3: set your size (this is necessary for autolayout; remember to set active to true)
imageView.widthAnchor.constraint(equalToConstant: 120).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 120).isActive = true

use the solution to avoid rookie mistake #1

from setup layout constraints

setLayoutConstraints(view: imageView, constraints: [
    imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
    imageView.widthAnchor.constraint(equalToConstant: 120),
    imageView.heightAnchor.constraint(equalToConstant: 120),
You can’t perform that action at this time.