Skip to content

Instantly share code, notes, and snippets.

@cameroncooke
Last active November 1, 2021 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameroncooke/a9244bc4d677f50940f5 to your computer and use it in GitHub Desktop.
Save cameroncooke/a9244bc4d677f50940f5 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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.

@justinmeiners
Copy link

This is a good idea, but I think you should be careful with using categories. This can affect a lot of classes you don't expect (such as Apple's mail controller, etc).

Note: You should always prefer subclassing to categories when modifying rotation behavior for UIKit classes such as UINavigationController. Because other classes may depend on the existing behavior of the UIKit container view controllers, the changes introduced by a category may cause unexpected behavior.

https://developer.apple.com/library/archive/qa/qa1688/_index.html

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