旋转时隐藏导航栏

6

我有一个带有导航栏的UIView。当用户将iPhone横向放置时,我该如何隐藏导航栏,并在纵向视图中再次显示它?

3个回答

9
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [[self navigationController] setNavigationBarHidden:UIInterfaceOrientationIsLandscape(toInterfaceOrientation) animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:UIInterfaceOrientationIsLandscape(toInterfaceOrientation) animated:YES];
}

8

在文档中很容易找到。在UINavigationController文档中,要隐藏导航栏,您可以使用:

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated

如果您希望在设备旋转时执行此操作,您需要在视图控制器方法中执行此操作(在UIViewController文档中有提到):
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

如果想要在屏幕方向改变时导航栏永远不会隐藏,但每当我切换到横向模式时它总是会隐藏,这段代码也无效。请帮帮我。 - Preeti Rani

-2
在我看来,更好的方法是使用CGAffineTransform,并将导航栏保留在原位 - 就像iPhone的照片库视图一样。请参阅Jeff Lamarche的优秀介绍Demystifying CGAffineTransform。对我帮助很大。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接