1

I got strange behaviour when setting the rootViewController programatically. I am using xib's only and here are scenarios of what I already tried. When I use this code, there is a small blink of black screen before it loads VC correctly.

- (void)setRootVC:(UIViewController *)viewController {
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];    
    [UIView transitionWithView:window
                      duration:0.0
                       options:UIViewAnimationOptionTransitionNone
                    animations:^{ window.rootViewController = viewController; }];
}

When I use different function, it eliminates the blink, but there's another strange behaviour. I got bunch of textfields in the new VC and I am setting one of them to becomeFirstResponder in viewDidLoad method, but when the VC loads, the textFieldDidEndEditing is called, which is totally strange. Here's the code.

- (void)setRootVC:(UIViewController *)viewController {
        UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
        [UIView transitionFromView:window.rootViewController.view
                             toView:viewController.view
                           duration:0.0                  
                            options:UIViewAnimationOptionTransitionNone
                         completion:^(BOOL finished){
                                    window.rootViewController = viewController;
                                    }];
}

I am restricted with objective-C, so swift solutions will not be helpful. Thanks for the replies.

Jozef Vrana
  • 309
  • 1
  • 5
  • 14
  • 1
    I think setting rootViewController should be done outside the animation block. Do the animation with viewController.view – Arun Jun 09 '17 at 15:02
  • One more doubt is if you are using UIViewAnimationOptionTransitionNone, then why can't you simply add give value to rootViewController without animation – Arun Jun 09 '17 at 15:03
  • There is blink of the black screen even when I add it outside of the animation block. I don't know what is causing it. But as I said, using the second code example, the blink is solved, but there is another thing wrong with it. I am using it inside animations, because I am also passing completion part, but I left it out from the examples. – Jozef Vrana Jun 09 '17 at 15:10
  • Try not using animation block. Just set the rootViewController – Arun Jun 09 '17 at 15:12
  • Already tried, the blink is still there and also I need animation for the completion part, even with any transition effect. – Jozef Vrana Jun 09 '17 at 15:20

1 Answers1

0

The blink is caused due to you are setting the rootViewController in completion where is it should be in animation block, so the code below might be helpful for you..

[UIView transitionWithView:self.window
                  duration:0.5
                   options:UIViewAnimationOptionTransitionNone
                animations:^{ self.window.rootViewController = viewController; }
                completion:nil];

Instead of using UIViewAnimationOptionTransitionNone you might want to add some transition effect so, you can use UIViewAnimationOptionTransitionCrossDissolve instead, it might look better..

Hope it helps.

Cheers.

iphonic
  • 12,615
  • 7
  • 60
  • 107