Ios iPhone锁定纵向方向

Ios iPhone锁定纵向方向,ios,orientation,Ios,Orientation,我意识到关于这个问题有一些线索。然而,我对这一点还不熟悉,我找到的一些解决方案对我并不适用 我有一个iPhone应用程序,除了一个视图外,应该支持所有方向。这个视图我只想锁定在肖像中。我试过这个代码,但当我翻动手机时,它仍然会进入横向模式。。。我做错了什么 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrient

我意识到关于这个问题有一些线索。然而,我对这一点还不熟悉,我找到的一些解决方案对我并不适用

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

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

注意:
shouldAutorotateToInterfaceOrientation
已在iOS 6.0中出现

现在,您应该覆盖演示文稿的
支持的界面定向
首选界面定向

像这样:

- (NSUInteger) supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

底线是,如果您使用的是UINavigationController,它通常不会将supportedInterfaceOrientations调用转发到俯视图控制器。子类UINavigationController并添加:

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

这似乎是最清楚的答案。还有其他方法,包括将代码放入应用程序代理中,使用类别将代码添加到UINavigationController等,但这似乎是我认为最干净的解决方案。

您必须将控制器锁定在RootViewController中(例如UITabbarController或UINavigationController)。当设备旋转时,首先它将调用AppDelegate的旋转方法(例如,-(BOOL)shouldAutorotate,-(nsInteger)supportedInterfaceOrientations)。然后它将调用RootViewController的旋转方法

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

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)支持的接口方向
方法中判断您的控制器,返回您想要支持的方向

尝试以下操作:

1-将这两个功能添加到应用程序委派。阅读评论了解更多信息

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() {}
}

我尝试了上面的代码,但当我翻动手机时,视图仍会进入横向模式。。。还有什么我需要做的吗?谢谢你是否从你的代码中删除了
shouldAutorotateToInterfaceOrientation
类?我在+1处找到了答案-很有趣,当你添加评论时,我正在阅读这篇文章。这是命中注定的!谢谢你的回答。我尝试了该代码,但收到一条错误消息,指出“Property topViewController not found…”您需要将此代码放入UINavigationController子类中,然后确保您的故事板引用该子类。
class YourViewController: UIViewController {

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