2
votes

I have a view controller that loads from a xib. I am doing NOTHING with the size in code - it was all set up in the XIB file. I have logging set showing that iniwithnibname has correct width and height, viewdidload has correct width/height, viewwillappear has wrong width/height.

The width/height are changing to values that do not exist in my code.

This is an iPad landscape only app, and running it in simulator on 7.1 shows it broken, and 8.2 shows it OK.

Can anyone shed some light on this?

here is how I am loading the VC with the xib:

    ShareViewController *shareViewController = [[ShareViewController alloc] initWithNibName:@"ShareViewController" bundle:nil imageName:imageSelected ];


    shareViewController.modalPresentationStyle = UIModalPresentationFormSheet;
    float fW = shareViewController.view.frame.size.width;
    float fH = shareViewController.view.frame.size.height;
    shareViewController.preferredContentSize = CGSizeMake(fW, fH);
    [self presentViewController:shareViewController animated:YES completion:^(){}];

In iOS8 simulator here is NSLog output:

KIStd[28103:762368] Entering ShareButtonPressed
KIStd[28103:762368] initWithNibName/image entry
KIStd[28103:762368] viewDidLoad entry
KIStd[28103:762368] viewDidLoad exit w:596.000000, h:400.000000
KIStd[28103:762368] initWithNibName/image exit w:596.000000, h:400.000000
KIStd[28103:762368] viewWillAppear Entry w:596.000000, h:400.000000
KIStd[28103:762368] viewWillAppear exit w:596.000000, h:400.000000
KIStd[28103:762368] viewDidAppear entry
KIStd[28103:762368] viewDidAppear exit w:596.000000, h:400.000000

In iOS7 simulator here is NSLog output:

KIStd[28186:60b] Entering ShareButtonPressed
KIStd[28186:60b] initWithNibName/image entry
KIStd[28186:60b] viewDidLoad entry
KIStd[28186:60b] viewDidLoad exit w:596.000000, h:400.000000
KIStd[28186:60b] initWithNibName/image exit w:596.000000, h:400.000000
KIStd[28186:60b] viewWillAppear Entry w:540.000000, h:620.000000
KIStd[28186:60b] viewWillAppear exit w:540.000000, h:620.000000
KIStd[28186:60b] viewDidAppear entry
KIStd[28186:60b] viewDidAppear exit w:540.000000, h:620.000000

This shows the problem quite clearly.

2
Did you use Layout constraints in your XIB ? If you didn't, layout behavior might be undefined...Vinzzz

2 Answers

7
votes

Never rely on frame and bound values in viewDidLoad/viewWillAppear. Usually the most precise location to listen to size changes is viewDidLayout, where you can keep track of the last seen size, and do your logic if the size changed.

viewDidAppear is precise, but is called after the view has already animated in, so you will see an ugly jump. viewWillLayout/viewDidLayout is usually the most precise one, as I mentioned before.

-1
votes

Maybe a problem with size classes? Try to configure (See About Designing for Multiple Size Classes) or disable it.

File Inspector - Size Classes