iOS 8: Presenting a modal view controller in portrait causes resize of the navigation bar of the underlying landscape navigation controller
Asked Answered
P

1

9

On iOS 8 I have a strange behavior regarding the navigation bar and orientation changes.

I have a navigation controller which reports a supported interface orientation UIInterfaceOrientationMaskLandscapeRight. The navigation bar has the expected height for landscape orientation (sadly I am not entitled to post screenshots).

Then I initiate a modal presentation of a view controller that only supports UIInterfaceOrientationMaskPortrait. When the presentation animation starts, it seems that the metrics of the underlying navigation controller are changed to a portrait presentation, as the height of the navigation bar grows to its portrait size, as depicted above.

iOS 7 does not exhibit this behavior. What am I missing? I want to restore the old behavior.

Here is the full code of the simple example above:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];


    DOGButtonViewController *root = [DOGButtonViewController new];
    DOGOrientedNavigationController *navi = [[DOGOrientedNavigationController alloc] initWithRootViewController:root];
    navi.allowedInterfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;

    self.window.rootViewController = navi;

    [self.window makeKeyAndVisible];
    return YES;
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}

@end


@implementation DOGOrientedNavigationController

- (NSUInteger)supportedInterfaceOrientations
{
    return self.allowedInterfaceOrientations;
}

@end

@implementation DOGButtonViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Button View Controller";
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (IBAction)buttonClicked:(id)sender
{
    DOGPortraitViewController *vc = [DOGPortraitViewController new];
    [self presentViewController:vc animated:YES completion:nil];
}

@end

@implementation DOGPortraitViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Portrait Title";
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (IBAction)buttonClicked:(id)sender
{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end

In a more complex setup I also experience the text in a UIWebView contained in the navigation controller being scaled up when presenting the portrait modal. When dismissing the modal, the text is not resized to its original size.

Platino answered 25/9, 2014 at 9:27 Comment(4)
Did you manage to find a solution to this? I'm experiencing the same thingMoises
Sadly not, but I will look into this in the upcoming days.Platino
Similar problem here. It looks like the presenting view controller also rotates (although it shouldn't)Blabber
Did you find a solution to this? I am experimening the same issue passing from a portinat to landscape.Punctilio
P
0

For lack of a better option I've done a bit of a hack for this. Basically before I show the modal view I take a screen shot and lay it on top of the presenting view controller.

Obviously I have to remove this screen shot when the view re-appears

  func showScreenShot () {
    let image = screenShot()
    self.screenShotImageView = UIImageView(image: image)
    self.view.addSubview(self.screenShotImageView!)
  }

func screenShot () -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, UIScreen.mainScreen().scale)
    self.view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image
}

func removeScreenShot () {
  if let screenImageView = self.screenShotImageView {
   screenImageView.removeFromSuperview()
   self.screenShotImageView = nil
  }
}
Peripatetic answered 24/11, 2014 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.