在我的应用程序中,根据视图控制器手动设置方向。除了这个视图控制器,我需要检测最顶视图控制器的当前方向(不是设备方向)。可能吗?
这就是我设置方向的方式。
-(BOOL)shouldAutorotate {
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
您可以通过以下方式设置每个视图控制器
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)
如上所述,您要检查root / top VC中的当前方向集。
所以你可以用这段代码检测: -
if (topViewController.responds(to: Selector(("canRotate")))) else{
// view is set to portrait
return .portrait;
}
// // view is set to landscape
return .allButUpsideDown;