开发者

The view is “jumping” when I add it to UIWindow as subview (via rootViewController property) and do a flip

开发者 https://www.devze.com 2023-03-01 07:14 出处:网络
I have two simple UIViewControllers, their views are 320 x 460 with status bars. I do in AppDelegate self.window.rootViewController = [[[SimpleController alloc] init] autorelease];

I have two simple UIViewControllers, their views are 320 x 460 with status bars. I do in AppDelegate

self.window.rootViewController = [[[SimpleController alloc] init] autorelease];

[self.window makeKeyAndVisible];

In SimpleController I have a button开发者_如何学运维 that does

- (IBAction) switchToVerySimpleController
{
  [UIView transitionWithView: [[UIApplication sharedApplication] keyWindow]
                    duration: 0.5
                     options: UIViewAnimationOptionTransitionFlipFromLeft
                  animations:^{ [[UIApplication sharedApplication] keyWindow].rootViewController = [[[VerySimpleController alloc] init] autorelease]; }
                  completion: NULL];
}

The new view (VerySimpleController.view) is filled with a blue color. After animation new view is shown with a tiny white stripe (with the size of a status bar) at the bottom and then it jumps down into place. Why is it happening and how to avoid that? I suppose its status bar to blame, and I tried to set statusBar = Unspecified in IB for both views, but it doesn't help.

UPDATE: When I hide statusBar (thru setting in .info file) from the start, no view adjustment occurs. But still... I need to show statusBar and I need that animation working properly.


When a rootViewController is assigned to a window, a new frame is assigned to the rootViewController's view if a status bar is present. This is so that the rootViewController's view won't be hidden under the status bar.

Since you're setting the window's rootViewController inside the animation block, the new frame assignment gets animated as well.

To not show the jump you might set the frame of the rootViewController's view before the animation with something like this:

- (IBAction) switchToVerySimpleController
{
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];

    VerySimpleController *vsc = [[[VerySimpleController alloc] init] autorelease];

    vsc.view.frame = CGRectMake(vsc.frame.origin.x,
                     statusBarFrame.size.height,
                     vsc.frame.size.width,
                     vsc.frame.size.height);

    [UIView transitionWithView: [[UIApplication sharedApplication] keyWindow]
                      duration: 0.5
                       options: UIViewAnimationOptionTransitionFlipFromLeft
                    animations:^{ [[UIApplication sharedApplication] keyWindow].rootViewController = vsc }
                    completion: NULL];
}


It seems that this issue still sometimes happen and it's probably a bug.

To avoid this 'jump' add this code to the viewWillAppear method of the view controller which needs to be shown:

swift

navigationController?.navigationBar.layer.removeAllAnimations()

objective-c

[self.navigationController.navigationBar.layer removeAllAnimations];

The transition will now finish without the 'jump'.

0

精彩评论

暂无评论...
验证码 换一张
取 消