I am trying to create an UIImageView
programmatically (so no storyboard/UI editor), and align it using anchor constraints.
The UIImageView should be aligned to the bottom and be scaled to fit the bottom area (like a full-width footer), but maintain aspect ratio, kind of like what is available in the Storyboard editor?
My current code in viewDidLoad()
looks like this, but no matter what I do, the image is displayed at the top and it seems the ImageView (not the image) fills the entire height of the parent view.
// Create and add image view
let imgView = UIImageView(image : UIImage(named : "Images/main_bottom.png"))
imgView.translatesAutoresizingMaskIntoConstraints = false
imgView.contentMode = .scaleAspectFit
view.addSubview(imgView)
// Align image view
let margins = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
imgView.leadingAnchor.constraint(equalTo: margins.leadingAnchor),
imgView.trailingAnchor.constraint(equalTo: margins.trailingAnchor),
imgView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])