CodexBloom - Programming Q&A Platform

How to implement guide with uiimageview not resizing correctly on iphone 14 on ios 16

👀 Views: 53 💬 Answers: 1 📅 Created: 2025-06-14
ios uiview uikit imageview autolayout swift

I need some guidance on Can someone help me understand I tried several approaches but none seem to work... I'm working with a frustrating scenario with UIImageView not resizing correctly on an iPhone 14 while running on iOS 16. I have a UIImageView that I want to fill its parent UIView, but it seems to have a fixed size regardless of the constraints I set. Here's the relevant part of my code: ```swift class MyViewController: UIViewController { let imageView = UIImageView() override func viewDidLoad() { super.viewDidLoad() setupImageView() } private func setupImageView() { imageView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(imageView) NSLayoutConstraint.activate([ imageView.topAnchor.constraint(equalTo: view.topAnchor), imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor), imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor), imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) imageView.contentMode = .scaleAspectFill imageView.image = UIImage(named: "exampleImage") } } ``` I've confirmed that `imageView` is being added to the view hierarchy and the constraints should ideally make it fill the entire view. However, on the iPhone 14, the image view appears to have a fixed height and width, which leads to it not scaling properly. I've tried using different content modes like `.scaleAspectFit`, but it doesn't resolve the question. The image appears squished and does not fill the available space as expected. Additionally, when I run the app, I notice the following warning in the debug console: `Warning: Attempt to present <UIViewController: ...> on <MyViewController: ...> whose view is not in the window hierarchy!`. I'm not sure if this is related but it seems to occur when the view is being laid out. I’ve also tried adding layoutSubviews manually without success. Has anyone encountered similar issues with UIImageView on iOS 16, or can someone help clarify what might be going wrong with the constraints or the image view setup? How would you solve this? I'm developing on Ubuntu 22.04 with Swift. Hoping someone can shed some light on this. Any advice would be much appreciated. I'm coming from a different tech stack and learning Swift. Is this even possible?