问题
I have a UIViewController
with the following code:
- (BOOL) shouldAutorotate {
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
I am not using a UINavigationController
. When this UIViewController
is being displayed, the device will still rotate to landscape. I am targeting iOS 9, what's the issue here?
回答1:
So the issue was that I had defined the allowed orientations in info.plist
which apparently overrides anything you do anywhere else throughout the project.
To correct the issue I removed the entries from info.plist
and defined them in the project settings. Now everything works as expected.
回答2:
I don't think Bryan's answer works,for changing the orientations in project settings also changes the info.plist
as @mrhangz commented.
If the issue is iOS9 only,it is probably due to the new feature of iOS9 in iPad called Split view
.The iOS9 enable Split view
by default in particular iPad device,see Apple documents here.
The split view
forced your app to support all orientations in all view once adoptted.So if you set all orientations support in either info.plist
or target general setting,and then split view
is supported by default,which will ignore the orientation setting though supportedInterfaceOrientations
in your viewController and support all orientations.
As the document written,if you checked Requires full screen
in your target settings,then your app will not support split view
.Now you can control orientations in code again.
回答3:
I have try many solution, but the correct answer with working solution is:
ios 8 and 9, no need to edit info.plist.
- (BOOL) shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
possible orientation
UIInterfaceOrientationUnknown
The orientation of the device cannot be determined.
UIInterfaceOrientationPortrait
The device is in portrait mode, with the device held upright and the home button on the bottom.
UIInterfaceOrientationPortraitUpsideDown
The device is in portrait mode but upside down, with the device held upright and the home button at the top.
UIInterfaceOrientationLandscapeLeft
The device is in landscape mode, with the device held upright and the home button on the left side.
UIInterfaceOrientationLandscapeRight
The device is in landscape mode, with the device held upright and the home button on the right side.
回答4:
In swift 5
The code below will lock the current view controller into portrait mode but still allow the other view controllers to transition to landscape. I do believe that you have to enable all the orientations at the project level and then turn then "off" using this method but am not sure if there is way to turn them back "on" one by one.
private var _orientations = UIInterfaceOrientationMask.portrait
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get { return self._orientations }
set { self._orientations = .portrait }
}
A more thorough explanation of it all can be found here: The supportedInterfaceOrientations method doesn't override any method from its superclass
回答5:
For simplicity, for iPad, if Supported interface orientations (iPad) property in info.plist includes all the four orientations, with UIRequiresFullScreen property value as NO, iOS will treat your app as supporting split view. If an app supports split view feature, you can not disable it from rotating, at least by the ways above.
I have a detail answer here.
来源:https://stackoverflow.com/questions/32782044/ios-9-supportedinterfaceorientations-not-working