问题
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