Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Propagates shouldAutorotate and supportedInterfaceOrientations calls to UITabBarController, UINavigationController and UISplitViewController.
//
// By Cameron Cooke at Brightec Ltd.
//
// Based on code from Stack Overflow: http://stackoverflow.com/a/15023156/248848
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
@implementation UITabBarController (Autorotation)
- (BOOL)shouldAutorotate
{
if (self.selectedViewController) {
return [self.selectedViewController shouldAutorotate];
}
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if (self.selectedViewController) {
return [self.selectedViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}
@end
@implementation UINavigationController (Autorotation)
- (BOOL)shouldAutorotate
{
if (self.visibleViewController) {
return [self.visibleViewController shouldAutorotate];
}
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if (self.visibleViewController) {
return [self.visibleViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}
@end
@implementation UISplitViewController (Autorotation)
- (BOOL)shouldAutorotate
{
if (self.viewControllers.count > 0) {
for (UIViewController *controller in self.viewControllers) {
if (![controller shouldAutorotate]) {
return NO;
}
}
}
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if (self.viewControllers.count > 0) {
return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}
@end
@cameroncooke

This comment has been minimized.

Copy link
Owner Author

@cameroncooke cameroncooke commented Oct 20, 2015

Just pop this category on the view controller that has a UINavigationController, UISplitViewController or UITabBarController ancestor and that you want to disable or control auto rotation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment