纵向和横向视图在同一iOS应用程序中共存

时间:2019-03-28 14:53:24

标签: ios objective-c

我有一个typedef控制多个UITabBarController

显示不同的UIViewControllers

所有视图中只有一个视图只能在肖像模式下显示,而不能旋转,而只有一个视图只能在风景模式下显示,而不能旋转。

这看似微不足道,但我无法实现……所有视图都处于纵向模式,或者所有视图均处于横向模式。它们要么全部旋转,要么都不旋转。

下面是“风景”视图的代码,但是编译器完全忽略了这些指令,并且在旋转设备时,视图会旋转为纵向模式。

UIViews

停止自动旋转的唯一方法是在IB的-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeRight; } -(BOOL) shouldAutorotate { return NO; } 中打一个框,然后将该设置应用于所有视图,并且所有视图都不会旋转。

我读到最近已经无法再对每个视图应用单独的设置了,这似乎是荒谬的。

有什么解决方法吗?

编辑了更多信息: @ManWithBeard,我无法弄清楚如何使您的代码适应我的要求。.我的UITabBarController控制不同的UIViewController,在其标题中定义如下:

Device Orientation

AppDelegate通过

进入第一个视图控制器
@interface Portrait_ViewController : UIViewController
@interface LandscapeViewController : UIViewController

然后从任何ViewContoller中,使用

移至所需的位置
[self.tabBarController setSelectedViewController: Portrait_ViewController]

我不明白如何按照您的建议将[self.tabBarController setSelectedIndex:index] 应用于UITabBarController的UIViewControllers。

2 个答案:

答案 0 :(得分:0)

应用程序仅要求顶级控制器提供界面方向信息。

在您的情况下,UITabBarController是顶级控制器,其他控制器是其子级。

您需要创建自己的UITabBarController子类来覆盖此方法,并从当前选定的控制器传递值。

此类容器的示例:
斯威夫特

class ChildOrientationTabBarController: UITabBarController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return selectedViewController?.supportedInterfaceOrientations ?? .portrait
    }
    override var shouldAutorotate: Bool {
        return selectedViewController?.shouldAutorotate ?? false
    }
}
class ChildOrientationNavigationController: UINavigationController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return topViewController?.supportedInterfaceOrientations ?? .portrait
    }
    override var shouldAutorotate: Bool {
        return topViewController?.shouldAutorotate ?? false
    }
}

Objective-C

@interface ChildOrientationTabBarController : UITabBarController
@end
@implementation ChildOrientationTabBarController

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.selectedViewController supportedInterfaceOrientations] ?: UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate] ?: NO;
}
@end

@interface ChildOrientationNavigationController : UINavigationController
@end
@implementation ChildOrientationNavigationController

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations] ?: UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate] ?: NO;
}
@end

并且AppDelegate应该将supportedInterfaceOrientation方法重写为:
斯威夫特

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return window?.rootViewController.supportedInterfaceOrientations ?? .portrait
}

Objective-C

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return [[window rootViewController] supportedInterfaceOrientations] ?: UIInterfaceOrientationMaskPortrait;
}

答案 1 :(得分:0)

  

我读到最近已经无法再对每个视图应用单独的设置了,这似乎是荒谬的。

这并不荒唐,确实如此。您不能使选项卡视图控制器的不同子代采用不同的强制方向。

迫使方向不同于当前方向的唯一方法是通过显示的视图控制器。