【问题标题】:How to hold NavigationController in subViewController orientation?如何将 NavigationController 保持在 subViewController 方向?
【发布时间】:2011-11-18 13:40:13
【问题描述】:

我已经制作了一个 NavigationController 结构。 FirstViewController 是 RootViewController,SecondViewController 是下一个 SubViewController。每个 ViewController shouldAutorotateToInterfaceOrientation 是不同的(参考以下代码)。 FirstViewController 只有纵向可用。 SecondViewController 都支持 Portrait 和 LandscapeMode。

在FirstViewController 中某按钮触摸后,调用pushHandler。下一个 SecondViewController 是推送的。 在将横向旋转回 FirstViewController 后的 SecondViewController 中,该方向太横向了。

但是,我实现的每个 ViewController 方向都不同。但是每个 ViewController 都不能独立设置方向。为什么会发生?

如何才能让viewcontroller每个方向都可以独立设置呢?

如果 SecondViewController 改变了 LandscapeMode 的方向。我想回到FirstViewController 还是portraitMode(永远保持portraitState 不受影响)。 如何以编程方式实现?

FirstViewController *rootViewController = [FirstViewController alloc] init];

UINavigationController *MyNavigationController = [UINavigationController alloc] initWithRootViewController:rootViewController]];

//FirstViewController

- (void)pushHandler
{
    SecondViewController *subViewController = [SecondViewController alloc] init];

    [[self navigationController] pushViewController:subViewController animated:YES];
    [subViewController release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//SecondViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{    
    return YES;
}

【问题讨论】:

    标签: iphone uinavigationcontroller orientation


    【解决方案1】:

    因为您为所有UIViewControllers 使用UINavigationController,所以您为UINavigationController 设置的任何方向行为都将应用于它的所有children,或者换句话说,它的堆栈上的对象。

    将UINavigationController 视为房子,将UIViewControllers 视为房间。房子里的所有房间都将以与房子相同的方式旋转。如果不转动房屋和其他房间,就无法转动房间。

    但总是有技巧。看我对这个问题的回答Only having one view autorotate in xcode?

    【讨论】:

    • 我已经尝试过应用类似的代码。我的 NavigationController 结构非常复杂,因为有一个 TabBarController、navigationItem ......您的代码不会更改 navigationItem 和 TabBarController 方向。和 [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(rotate) name:UIDeviceOrientationDidChangeNotification object:nil];此通知始终仅响应 UIDeviceState。不相关的 shouldAutorotateToInterface。 return (interfaceOrientation == UIInterfaceOrientationPortrait);但通知总是响应。所以你的代码不适合我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    相关资源
    最近更新 更多