On iOS, why does the viewing frame height remain almost the same after rotation?

In ViewController.m , on the iPad, if we print the view frame height in the touch event handler:

 NSLog(@"Height of main view is %f", self.view.frame.size.height); 

then in portrait mode, the value is 1004 (20 pixels for the status bar of the device, so 1024 - 20 = 1004), and if the device is in landscape mode, I expected it to be around 768 or 748, but the value actually printed is 1024. (update: if the application is launched in landscape mode, then without rotation it will also be printed as 1024). Why is this so, and is there a rule for expecting a value of 748 or 768? (Is there more than one way to get this?)

+6
source share
3 answers

In Apple docs about UIView frame:

Warning. If the transformation property is not an identity transformation, the value of this property is undefined and should therefore be ignored.

So you should use the bounds property.

+4
source

Instead, you can control the orientation programmatically and overwrite the willRotateToInterfaceOrientation method. See Method:

 #define IPAD_SIZE_PORTRAIT CGSizeMake(768, 960) #define IPAD_SIZE_LANDSCAPE CGSizeMake(1024, 704) -(void)relayoutSubviews:(UIInterfaceOrientation)orientation { CGSize scrSize; if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) { scrSize = IPAD_SIZE_LANDSCAPE; } else { scrSize = IPAD_SIZE_PORTRAIT; } self.view.bounds = CGRectMake(0, 0, scrSize.width, scrSize.height); self.view.frame = CGRectMake(0, 0, scrSize.width, scrSize.height); } -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [self relayoutSubviews:toInterfaceOrientation]; } 
+1
source

The rationale for this is that when you rotate the device, the presentation frame changes to fit on the status bar, but does not actually rotate the presentation frame. The device simply displays the view in a rotating state, while the view does not actually rotate. I believe this change affected subviews, but the general view is not like that.

0
source

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


All Articles