Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewWillTransitionToSize and wrong navigationBar and statusBarFrame heights

I am trying to get the visible drawing area for a subclass of GLKViewController. In iOS 7, I would determine the orientation and then adjust [UIScreen mainScreen].bounds.size accordingly. Then I would subtract the navigationBar and statusBarFrame heights and voila! - the actual size of my drawing area.

Now I'm trying to update for iOS 8 with viewWillTransitionToSize, however when I rotate the device (I have UIInterfaceOrientationMaskAll on), the size that is passed is wrong. In fact, the width is correct for the view, but not the height (which involves the navbar and statusbar heights).

I've seen a couple of questions on SO that relate but nothing that actually deals with this issue, at least nothing that helped me.

This doesn't seem to me to be doing anything interesting, correct me if I'm wrong:

- (void)viewWillTransitionToSize : (CGSize) size
       withTransitionCoordinator : (id<UIViewControllerTransitionCoordinator>) coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    NSLog(@"size changed : orig(%f,%f) mainScreen(%f,%f)", size.width, size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

    [self updateViews:size];
}

outputs for transitioning to landscape:

size changed : orig(568.000000,256.000000) mainScreen(320.000000,568.000000)

where the height passed from viewWillTransitionToSize appears to be:

320 - (2*32) = 256

and then the next rotation to portrait:

size changed : orig(320.000000,536.000000) mainScreen(568.000000,320.000000)

where the height passed from viewWillTransitionToSize appears to be:

568 - 32 = 536

32 happens to be the height of the navbar in landscape mode (if that means anything).

So my questions are: how does viewWillTransitionToSize get this size? How does it take into account the extra bars that change sizes or disappear depending on the orientation? And most importantly, why is it using these incorrect heights?

like image 667
dragonflyesque Avatar asked Mar 24 '15 21:03

dragonflyesque


1 Answers

When looking at view frame sizes viewWillTransitionToSize:withTransitionCoordinationar:, you will likely get the frames before the update.

Better use animateAlongsideTransition:completion: inside this method:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size
          withTransitionCoordinator:coordinator];


    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
          //update views here, e.g. calculate your view
    }
                                 completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {
     }];
}
like image 107
bhr Avatar answered Sep 21 '22 12:09

bhr