How Do I detect the orientation of the device on iOS?

后端 未结 12 912
小鲜肉
小鲜肉 2020-11-30 01:56

I have a question on how to detect the device orientation on iOS. I don\'t need to receive change notifications, just the current orientation itself. This seems to be a rath

12条回答
  •  自闭症患者
    2020-11-30 02:52

    My current way of doing this:

    + (BOOL)isPortrait {
        let window = UIApplication.sharedApplication.delegate.window;
        if(window.rootViewController) {
            let orientation =
            window.rootViewController.interfaceOrientation;
            return UIInterfaceOrientationIsPortrait(orientation);
        } else {
            let orientation =
            UIApplication.sharedApplication.statusBarOrientation;
            return UIInterfaceOrientationIsPortrait(orientation);
        }
    }
    

    If there is for some reason no rootViewController yet fail safe to statusBarOrientation...

提交回复
热议问题