Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 - After pushing a UIViewController on a UINavigationController and rotating the device, the size is wrong in previous view controller

I have a UINavigationController with a root UIViewController ("root").

The root view controller pushes another UIViewController "child". When the "child" UIViewController is on the screen , I rotate the device and expect the "root" view controller to resize accordingly but this isn't happening. After putting a breakpoint in the root view controller:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

I see that the size is wrong and the root view controller doesn't adjust properly to the change.

Has any one experienced this behaviour?

The code is as so:

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


}

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
/// The size is wrong if this view controller is off screen
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

enter image description here

Here is a print screen of the NSLog of the size after rotating the device - This is from the simulator but the behaviour is the same on the device.

enter image description here

like image 740
Avner Barr Avatar asked Nov 12 '14 15:11

Avner Barr


1 Answers

Same issue on my project. It seems that UINavigationController and UITabBarController (maybe all viewController?) give to there children the wrong size when you call :

'[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];'

I fixed the issue by overriding 'viewWillTransitionToSize:withTransitionCoordinator:' in my tabBarController and navigation bar controller subclass (same code in both).

- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    for (UIViewController* aViewController in self.viewControllers)
    {
        [aViewController viewWillTransitionToSize:size
                        withTransitionCoordinator:coordinator];
    }

    if ([self presentedViewController])
    {
        [[self presentedViewController] viewWillTransitionToSize:size
                                       withTransitionCoordinator:coordinator];
    }
}

I'm not sure this is the best way, if you found something better, tell me please.

like image 138
Raphaël Pinto Avatar answered Oct 04 '22 16:10

Raphaël Pinto