Orientation of screen?

痞子三分冷 提交于 2019-12-11 12:08:43

问题


How do I make sure that the only orientations allowed are Portrait or Upside down portrait?

I made the change in the project settings, but I'm having trouble making the change in my GameViewController.

I need to make the change in this function:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .AllButUpsideDown
    } else {
        return .All
    }
}

However, the "UIInterfaceOrientationMask" structure doesn't have an option that only allows portrait and upside down portrait.


回答1:


You have to allow all orientations in the project settings, then only orientations allowed are Portrait or Upside down portrait in your viewController except the GameViewController:

// BaseViewController
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return UIInterfaceOrientationMask.Portrait.rawValue | UIInterfaceOrientationMask.PortraitUpsideDown.rawValue
    } else {
        return .All
    }
}

Finally in the GameViewController:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .AllButUpsideDown
    } else {
        return .All
    }
}



回答2:


You might want to try:

return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown)

Not sure if Swift will support a bitwise operator.



来源:https://stackoverflow.com/questions/30836724/orientation-of-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!