ViewWillTransitionToSize and wrong navigation height Bar and statusBarFrame

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 height of the navigationBar and statusBarFrame and voila! - The actual size of the drawing area.

Now I'm trying to update iOS 8 using viewWillTransitionToSize, however, when I rotate the device (I have UIInterfaceOrientationMaskAll), the size that is transferred is incorrect. In fact, the width is true for the view, but not the height (which includes the height of the navigation bar and the height of the status bar).

I saw a couple of questions about SO that are relevant, but nothing that really deals with this problem, at least nothing that helped me.

It seems to me that I'm not doing anything interesting, correct me if I am 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]; } 

to go to the landscape:

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

where the height traveled from viewWillTransitionToSize is as follows:

 320 - (2*32) = 256 

and then the following rotation to the portrait:

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

where the height traveled from viewWillTransitionToSize is as follows:

 568 - 32 = 536 

32 - the height of the navigation bar in landscape mode (if it means something).

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

+5
source share
1 answer

When viewing frame sizes viewWillTransitionToSize:withTransitionCoordinationar: you are likely to get frames before updating.

It is better to 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, eg calculate your view } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { }]; } 
+12
source

Source: https://habr.com/ru/post/1216025/


All Articles