1

I've set a UIImageView in my view controller and assigned constraints x & Y which works I'm trying to set the image width, height & aspect ratio but it doesn't seem to be working.

I've tried many answers already on here but can seem to get it working.

import UIKit

class GuestLoginViewController: UIViewController {

    let headerImage = UIImageView()

    override func viewDidLoad() {
        super.viewDidLoad()

        setHeaderImage()
    }

    func setHeaderImage() {
        view.addSubview(headerImage)
        headerImage.frame = CGRect(x: 0, y: 0, width: super.view.frame.width, height: 95)
        headerImage.translatesAutoresizingMaskIntoConstraints = false
        headerImage.mask?.contentMode = UIView.ContentMode.redraw
        headerImage.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        headerImage.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        headerImage.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

        headerImage.image = UIImage(named: "header")
        view.sendSubviewToBack(headerImage)
    }


}
8
  • Did you try to change this line before the last line? headerImage.image = UIImage(named: "header") view.addSubview(headerImage) view.sendSubviewToBack(headerImage) or you can call headerImage.setNeedsLayout() Commented May 17, 2019 at 19:44
  • @ValNolav I already have view.addSubview(headerImage) it's the first line in my function. I moved it after the headerImage.image = UIImage(named: "header") line but I then get error "terminating with uncaught exception of type NSException" Commented May 17, 2019 at 20:00
  • It is because of constraints line you are adding. You need to move view.addSubview(headerImage) above adding constraints line headerImage.topAnchor.constraint(equalTo: view.topAnchor).isActive = true Commented May 17, 2019 at 20:13
  • @ValNolav still doesn't work even if move the line above the constraints or remove the constraints line the image is too big / height. imgur.com/a/M3ZatHA Commented May 17, 2019 at 20:27
  • 1
    As soon as you set headerImage.translatesAutoresizingMaskIntoConstraints = false, the frame is ignored. You need to set some constraint to establish the height of your UIImageView. Unfortunately, the image contents does not affect the height of the UIImageView. Set either 1) an absolute height constraint, 2) an offset from the bottom of the superView, 3) height relative to the width with a multiplier (an "aspect ratio constraint"). Commented May 17, 2019 at 20:46

1 Answer 1

2

As soon as you set headerImage.translatesAutoresizingMaskIntoConstraints = false, the frame is ignored. You need to set some constraint to establish the height of your UIImageView. Unfortunately, the image contents does not affect the height of the UIImageView.

Set either:

  1. an absolute height constraint
  2. an offset from the bottom of the superView
  3. height relative to the width with a multiplier (an "aspect ratio constraint")

Based on my comment, you turned off headerImage.translatesAutoresizingMaskIntoConstraints = false and it worked.

This gives you extra constraints (your 3 plus the 4 that are generated from the frame), but luckily they aren't conflicting.

Instead, I would suggest you leave the translatesAutoresizingMaskIntoConstraints set to false and set a constraint for the height:

headerImage.heightAnchor.constraint(equalToConstant: 95).isActive = true
Sign up to request clarification or add additional context in comments.

1 Comment

Ah ok, that makes sense now. I thought headerImage.frame = CGRect(x: 0, y: 0, width: super.view.frame.width, height: 195) was setting the height. I didn't know that I had to set the constraint height too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.