显示导航栏时 viewWillAppear 中的正确 self.view.frame
Posted
技术标签:
【中文标题】显示导航栏时 viewWillAppear 中的正确 self.view.frame【英文标题】:Proper self.view.frame in viewWillAppear while showing navigationBar 【发布时间】:2012-10-03 11:37:07 【问题描述】:我在父视图控制器中显示navigationBar
:
- (void)viewWillDisappear:(BOOL)animated
[self.navigationController setNavigationBarHidden:NO animated:YES];
[super viewWillDisappear:animated];
然后,在下一个视图控制器中
- (void)viewDidLoad
/* ... */
NSLog(@"%i, %g", self.navigationController.navigationBarHidden,
self.view.frame.size.height);
- (void)viewWillAppear:(BOOL)animated
/* ... */
[super viewWillAppear:animated];
NSLog(@"%i, %g", self.navigationController.navigationBarHidden,
self.view.frame.size.height);
- (void)viewWillLayoutSubviews
NSLog(@"%i, %g", self.navigationController.navigationBarHidden,
self.view.frame.size.height);
- (void)viewDidAppear:(BOOL)animated
NSLog(@"%i, %g", self.navigationController.navigationBarHidden,
self.view.frame.size.height);
输出:
-[viewDidLoad]: 1, 416
-[viewWillAppear:]: 0, 460
-[viewWillLayoutSubviews]: 0, 416
-[viewDidAppear:]: 0, 416
如您所见,viewWillAppear
出错了self.view.frame.size.height
。可以使用viewWillLayoutSubviews
,但它是在ios5中引入的。是否可以在 viewWillAppear 中获得适当的帧高度?
【问题讨论】:
为什么 460 错了? 480-20(状态栏高度)= 460 抱歉,我正在显示导航栏;) 【参考方案1】:这是迄今为止我得到的最佳解决方案:
- (void)viewWillAppear:(BOOL)animated
/* recalculate frame size */
CGSize size = [UIScreen mainScreen].bounds.size;
UIApplication *application = [UIApplication sharedApplication];
if (UIInterfaceOrientationIsLandscape(application.statusBarOrientation))
size = CGSizeMake(size.height, size.width);
if (!application.statusBarHidden)
size.height -= MIN(application.statusBarFrame.size.width,
application.statusBarFrame.size.height);
CGRect frame = self.view.frame;
frame.size.height = size.height -
self.navigationController.navigationBar.frame.size.height;
self.view.frame = frame;
/* ... */
【讨论】:
以上是关于显示导航栏时 viewWillAppear 中的正确 self.view.frame的主要内容,如果未能解决你的问题,请参考以下文章
在 iOS 7 中使用 Tint Color 显示导航栏时白色状态栏消失
当我在小于 10 的 ios 版本中隐藏/显示导航栏时,应用程序在进入后台后卡住