iphone - UITabBarController 与 viewControllers 利用不同的方向?

标签 iphone uinavigationcontroller uitabbarcontroller uiinterfaceorientation

我可以看到这是困扰很多人的事情:/

我有一个 UITabBarController,它有 4 个 viewController,全部都是 UINavigationController 类型。 导航 Controller 之一将 View Controller 推送到其堆栈上,该 View Controller 应以横向模式/方向呈现。

viewController 是一个图表,它是应用程序中景观唯一有意义的地方。 (当出现 UITabBar 时,我会隐藏它,以免让用户相信这在任何地方都有效)

为了使 UITabBarController 正确响应方向的变化,其所有 viewController 需要从委托(delegate)方法返回相同的值:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

因此,为了适应这种行为,我在属于 UITabBarController 的所有 viewController 中实现了此方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL canRotate = [defaults boolForKey:@"can_rotate"];

    return canRotate;
}

现在的“技巧”是,当我的 can-be-landscape viewController 被推送时,我会这样做:

- (void) viewWillAppear:(BOOL)animated {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:YES forKey:@"can_rotate"];
    [defaults synchronize];

}

当它弹出时,我这样做:

- (void) viewWillDisappear:(BOOL)animated {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:NO forKey:@"can_rotate"];
    [defaults synchronize];

}

这个效果非常好。当 viewController 位于堆栈上时,我可以旋转设备, View 也会随之旋转。

但是问题是,如果用户在横向模式下点击导航栏上的“后退”按钮,从而将 viewController 弹出到前一个 viewController,那么这个“旧”viewController 当然也是处于横向模式。更糟糕的是,因为我将 BOOL 设置为 NO,所以当我将设备定向为纵向模式时,这个“旧”viewController 无法旋转回去。

有没有办法更新所有内容,以便当我弹出可横向模态视图 Controller 时,我的其他 View Controller 都不会处于横向模式?

我有点担心,如果这可以从横向到纵向完成,那么从纵向到横向也应该是可能的,从而使我的“黑客”变得不必要..但如果不能,那么我又回到了原点:/

希望我很接近并且有人可以帮助我到达那里,谢谢:)

最佳答案

我想我可以简化整个事情......

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
return NO;
}<p></p>

<p>-(void) viewDidAppear:(BOOL)animated {
        [UIView beginAnimations:@"View Flip" context:nil];
        [UIView setAnimationDuration:0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];</p>

<pre><code>    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(90.0*0.0174532925);
    self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
    self.view.center = CGPointMake(160.0f, 240.0f);
</code></pre>

<p>//  [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;</p>

<pre><code>[UIView commitAnimations];
[super viewDidAppear:animated];
</code></pre>

<p>}</p>

<p></p>

不会自动旋转任何内容,但您可以手动旋转要移动的 View 。如果您不需要 View 中的键盘支持,则无需更改 statusBarOrientation。

如果您并不总是希望旋转该 View ,那么您会遇到一个稍微困难的问题 - 您可能需要检查加速度计值以找出手机的方向(或者可能暗示手机的方向)已通过调用 shouldAutotateToInterfaceOrientation 进行转换,然后在需要时调用转换代码。

关于iphone - UITabBarController 与 viewControllers 利用不同的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3009206/

相关文章:

swift - UITabBarController 顶部的空白

ios - 在标签栏中心添加自定义按钮的最佳方式

swift - 如何在 swift 中使用 UITabBar 导航 View Controller ?

ios - 在没有 contentView 框架的情况下计算 UITableViewCell 高度的最佳方法

ios - 在iPhone上以编程方式打开蓝牙

iphone - 如何更改 UIActionSheet View ?

ios - 像藤蔓一样的导航 Controller

iphone - 如何将我未发布的 iPhone 应用程序提供给不在我附近的人进行测试?

ios - 像 Facebook Iphone 应用程序一样的滑动 ViewController 动画

uinavigationcontroller - UISplitView 左侧的多级导航 Controller ,带有一个小扭曲