viewWillTransitionToSize and wrong navigationBar and statusBarFrame heights

感情迁移 提交于 2019-12-22 05:33:46

问题


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?


回答1:


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)
     {
     }];
}


来源:https://stackoverflow.com/questions/29242916/viewwilltransitiontosize-and-wrong-navigationbar-and-statusbarframe-heights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!