iPhone锁定纵向方向

时间:2014-02-28 20:24:35

标签: ios orientation

我意识到这个问题有几个主题。但是,我是新手,我发现的一些解决方案对我不起作用。

我有一个iPhone应用程序应该支持所有方向,除了在一个视图中。这个观点我只想锁定肖像。我尝试了这段代码,但是当我翻动手机时它仍然会进入横向模式......我做错了什么?

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

4 个答案:

答案 0 :(得分:0)

注意:iOS 6.0中的shouldAutorotateToInterfaceOrientation已为deprecated

您现在应该覆盖supportedInterfaceOrientationpreferredInterfaceOrientationForPresentation类。

像这样:

- (NSUInteger) supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

答案 1 :(得分:0)

最重要的是,如果你正在使用UINavigationController,它通常不会将supportedInterfaceOrientations调用转发给顶层视图控制器。子类UINavigationController并添加:

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

似乎是最干净的答案。还有其他方法涉及将代码放入您的app委托,使用类别将代码添加到UINavigationController等,但这对我来说似乎是最干净的解决方案。

答案 2 :(得分:0)

您必须将控制器锁定在RootViewController中(例如,UITabbarController或UINavigationController)。当你的设备旋转时,第一个它将调用AppDelegate的rotateMethod(例如 - (BOOL)shouldAutorotate, - (NSUInteger)supportedInterfaceOrientations)。然后它调用RootViewController的RotateMehod。

1.如果您的RootViewController是UITabbarController,请在UITabbarController中添加如下代码的代码(NotRotateController是您不想进入横向模式的控制器)

#import "NotRotateController.h"
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    UINavigationController * baseNavCtr = self.viewControllers[self.selectedIndex];
    if ([baseNavCtr.topViewController isKindOfClass:[NotRotateController class]]) {
        return UIInterfaceOrientationMaskPortrait;
    }else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

2.如果你的RootViewController是UINavigationContoller

#import "NotRotateController.h"

- (NSUInteger)supportedInterfaceOrientations
{
    if (self.topViewController isKindOfClass:[NotRotateController class]) {
        return UIInterfaceOrientationMaskPortrait;
    }else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

- (BOOL)shouldAutorotate {
    return YES;
}

3. 总结,在RootViewController'- (NSUInteger)supportedInterfaceOrientations方法中判断您的Controller,返回您想要的方向

答案 3 :(得分:0)

尝试一下:

1-将这两个功能添加到App Delegate。阅读评论以获取更多信息

class AppDelegate: UIResponder, UIApplicationDelegate {

    // 1.
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

        if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {

            if (rootViewController.responds(to: Selector(("canRotate")))) {
                // Unlock landscape view orientations for this view controller
                return .allButUpsideDown
            }

            // *** THIS WILL GET CALLED WHEN THE VC CALLS THIS SELECTOR ***
            if (rootViewController.responds(to: Selector(("cantRotate")))) {
                // Unlock portrait only view orientation for this view controller
                return .portrait
            }
        }

        // Allow all orientations but upside down
        return .allButUpsideDown
    }

    // 2.
    private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
        if (rootViewController == nil) { return nil }
        if (rootViewController.isKind(of: UITabBarController.self)) {
            return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
        } else if (rootViewController.isKind(of: UINavigationController.self)) {
            return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
        } else if (rootViewController.presentedViewController != nil) {
            return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
        }
        return rootViewController
    }
}

2-将其添加到您要仅用作纵向的vc中:

class YourViewController: UIViewController {

    // MARK: - Portrait Only. The first function above inside App Delegate responds to this
    @objc func cantRotate() {}
}