IOS8 iOS7 and iOS6 prevent modally presented view controller from rotating

I have multiple view controllers that I present using the currentViewController: animated: method. I don't want the app to rotate while you present them on iPad as they are all blurred by the background using FXBlurView which is a mess to rotate and also because sometimes rotation of the presented view messes up the presentation.

I need to support iOS6, 7 and 8 and couldn't find any solution near the private API setOrientation: method.

+3


source to share


1 answer


The only solution I have found is to subclass the navigation controller and contain a modal view controller in that navigation controller.
This means that you will have to present a navigation controller that will have the root view controller that you originally wanted to expose.

Below is an example implementation of a non-rotating (portrait) navigation controller:

@implementation NonRotatingNavigationController

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

@end

      

You can also "ask" the displayed view controller instead of determining which orientation is supported.
Like this:

@implementation NonRotatingNavigationController

- (BOOL)shouldAutorotate {
    return [[[self viewControllers] lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations {
    return [[[self viewControllers] lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[[self viewControllers] lastObject] preferredInterfaceOrientationForPresentation];
}

@end

      




EDIT (Added alternative solution)

Another solution is to add a separate window that will contain the view controller.
This time, you will have to implement rotation methods inside the view controller itself and set it as the root view controller of the new window.

Here is the code for creating the window (it is important that the window is owned by someone - for example a strong property in the original view controller - otherwise it will be released and you won't see anything):

@interface ViewController ()
@property (nonatomic, strong) UIWindow *modalWindow;
@end

@implementation ViewController

- (IBAction)buttonTapped:(id)sender {

    NonRotatingViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"NonRotatingViewController"];

    UIWindow *modalWindow = [[UIWindow alloc] initWithFrame:self.view.window.frame];
    modalWindow.windowLevel = UIWindowLevelAlert;
    modalWindow.backgroundColor = [UIColor clearColor];
    [modalWindow setRootViewController:vc];
    [modalWindow makeKeyAndVisible];

    self.modalWindow = modalWindow;

}

@end

      

+1


source







All Articles