Joh*_*rug 5 uinavigationbar orientation uiinterfaceorientation ios landscape-portrait
我有一个简单的问题:我希望UINavigationBar在我的设备处于纵向状态时找出景观的高度.这是可能的,如果是的话,怎么样?
一些背景:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
[navBar sizeToFit];
NSLog(@"navBar: %@", NSStringFromCGRect(navBar.frame));
Run Code Online (Sandbox Code Playgroud)
这将返回正确的高度为当前设备的方向,例如44.这意味着,UINavigationBar的sizeToFit方法必须以某种方式看待当前设备的方向.有没有什么方法可以找到景观中的高度而不去景观?
好的,这是一种可行的解决方案:
@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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1319 次 |
| 最近记录: |