0

In Interface Builder I have a simple view that contains a UIImageView and am trying to constrain the image view's width and height. I have added constraints several ways but whenever I run in the Simulator the image view does not display correctly but always adopts the dimensions of the PNG image, whose dimensions are smaller than the simulator's device. The Content Mode for the image view is Scale to Fill. I am loading the xib like this:

VCMain *vc = [[VCMain alloc] initWithNibName:@"Main" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
navControllermodalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:navController animated:YES completion:nil];

I have tried constraining the image view to the superview, and constraining it to the Safe Area, but nothing changes. Regardless of which I choose console printing reveals that the Safe Area always takes on the dimensions of the image view (which oddly takes on the dimensions of the png file, even though the setting is Scale to Fill).

I also tested assigning an image larger than the Simulator's dimensions (using iPhone Xs btw) and smaller. You can see in the pics below that the image view does not conform to the constraints.

What am I doing wrong? And why is the Safe Area conforming rather to the image size? Below are pics of IB that show settings, and then pics showing how the Simulator renders things.

settings in IB

When I use an image smaller than the Simulator device dimensions I get this:

and though you can't see, the Safe Area dimensions are {{0, 0}, {320, 480}}, exactly the size of the PNG.

When I use a version of the image much larger than the Simulator device's dims I get:

and again the Safe Area dimensions are exactly the size of the PNG, in this case {{0, 0}, {2133, 2789}}, even though the main view there has dims of {{0, 0}, {375, 812}}

Alyoshak
  • 2,696
  • 10
  • 43
  • 70
  • Show how you are loading the xib and what you are then doing. Are you adding the xib view in Storyboard / IB? Are you loading via code and setting constraints? – DonMag Apr 02 '20 at 14:32
  • @DonMag, I edited the question to show how I'm loading the xib. The 4 constraints are part of the xib. Not doing anything after the loading and presentation code, and my print to console that shows the safe area dims is in viewDidAppear, so everything's wrapped up at that point. – Alyoshak Apr 02 '20 at 16:18
  • Sorry, you added code for presenting a navigation controller. Where is the code for loading your xib that contains your image view? – DonMag Apr 02 '20 at 20:05
  • There, I added it. Sorry about that. – Alyoshak Apr 03 '20 at 15:24
  • hmmm... tough to say where you may have gone wrong. Quick test, and it worked without problem for me. I put up a complete project that you can check here: https://github.com/DonMag/ViewControllerFromXIB – DonMag Apr 03 '20 at 15:55
  • Well... that's a bit odd, as it has nothing to do with Storyboards. Did you look at the example project I put up? Did you see anything different? – DonMag Apr 06 '20 at 16:54
  • Thanks to your sample project @DonMag I figured out to fix this. Your project used "Translates Mask Into Constraints" set instead of "Automatic", for the main view. I also had to set Autoresizing "struts" and turn off the "springs", so that it was like yours. But it is mystifying why this worked -- the safe area should not be modified because I chose not to use autoresiging, should it? Anyow, if you want to add this answer as a question I can mark it correct, and then hopefully it will help others here. – Alyoshak Apr 07 '20 at 21:05

0 Answers0