如何将 NavigationController 保持在 subViewController 方向?

发布于 2024-12-16 22:23:14 字数 1560 浏览 0 评论 0原文

我制作了一个 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;
}

I have made a NavigationController structure.
FirstViewController is RootViewController, SecondViewController is a next SubViewController. Each ViewController shouldAutorotateToInterfaceOrientation is a different(refer a following code).
FirstViewController is only portrait available. SecondViewController all support Portrait and landscapeMode.

In FirstViewController after sth button touch, call a pushHandler. next SecondViewController is a pushed.
In SecondViewController after rotated landscape back to the FirstViewController, that orientation is too landscape.

but, I implemented each ViewController orientation different. however each ViewController not independently set orientation. why happen?

How can I viewcontroller each orientation can be set independently, to do?

If SecondViewController change the orientation of the landscapeMode. I want back to the FirstViewController still portraitMode(forever hold portraitState Anything not to be affected).
How do I implements programmatically?

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-12-23 22:23:14

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

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

但一如既往,还是有技巧的。看看我对这个问题的回答 在 xcode 中只有一个视图自动旋转?

Because you use a UINavigationController for all your UIViewControllers whatever orientation behaviour you will set for the UINavigationController will be applied to all it's children or in otherwords objects on its stack.

Think of a UINavigationController as a house and UIViewControllers as rooms. All the rooms in the house will rotate in the same way the house. There is no way to turn a room without turning the house and other rooms.

But as always there are tricks. Look at my answer to this question Only having one view autorotate in xcode?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文