如何检测视图控制器iOS的当前方向?

问题描述 投票:0回答:2

在我的应用程序中,根据视图控制器手动设置方向。除了这个视图控制器,我需要检测最顶视图控制器的当前方向(不是设备方向)。可能吗?

这就是我设置方向的方式。

-(BOOL)shouldAutorotate {
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}
ios objective-c screen-orientation
2个回答
0
投票

您可以通过以下方式设置每个视图控制器

class YourViewController: UIViewController {

    var _orientations = UIInterfaceOrientationMask.landscape
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    get { return self._orientations }
    set { self._orientations = newValue }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    //...
}

然后在任何地方阅读此属性:

let vc = YourViewController()
print(vc._orientation)

0
投票

如上所述,您要检查root / top VC中的当前方向集。

所以你可以用这段代码检测: -

if (topViewController.responds(to: Selector(("canRotate")))) else{
        // view is set to portrait 
        return .portrait;
    }
    // // view is set to landscape 
    return .allButUpsideDown;
© www.soinside.com 2019 - 2024. All rights reserved.