Get the height of the UINavigationBar terrain when the device is in portrait

I have a simple question: I want to know the height of the UINavigationBar in the landscape while my device is in portrait. Is this possible, and if so, how?

Some background:

 UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectZero]; [navBar sizeToFit]; NSLog(@"navBar: %@", NSStringFromCGRect(navBar.frame)); 

This returns the correct height for the current device orientation , for example 44 . This means that the UINavigationBar sizeToFit method must somehow look at the current orientation of the device. Is there any way to know what the height will be in the landscape without going to the landscape?

+5
source share
2 answers

OK, this is one possible solution that works:

 @interface TestVC : UIViewController @property (assign, nonatomic) UIInterfaceOrientationMask orientationMask; @end @implementation TestVC - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return _orientationMask; } @end @implementation ViewController - (IBAction)getNavigationBarHeight:(id)sender { TestVC *vc = [[TestVC alloc] init]; if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { vc.orientationMask = UIInterfaceOrientationMaskLandscapeLeft; } else { vc.orientationMask = UIInterfaceOrientationMaskPortrait; } [self presentViewController:vc animated:NO completion:^{ UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectZero]; [navBar sizeToFit]; NSLog(@"navBar frame in 'opposite' orientation: %@", NSStringFromCGRect(navBar.frame)); }]; [self dismissViewControllerAnimated:NO completion:nil]; } @end 
0
source

Why not grab and set this information in willAnimateRotationToInterfaceOrientation. Overriding this method, you should be able to get the information you want before the view appears.

In other words, don't worry about it until you actually change your orientation.

willAnimateRotationToInterfaceOrientation

+2
source

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


All Articles