Why does hiding my status bar completely break my

2019-03-06 13:29发布

问题:

Example project: http://cl.ly/0O2t051o081p

I want to achieve a slide-out sidebar as described in this question where the status bar moves as well. I've got everything working and the sidebar slides, but as soon as I try to hide the status bar with to complete the "illusion" it stops anything from happening at all and the sidebar no longer slides out.

Here's my code:

- (void)viewDidAppear:(BOOL)animated {
    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.PostsView addSubview:[[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO]];

        hide = YES;
        [self setNeedsStatusBarAppearanceUpdate];

        [UIView animateWithDuration:1.0 animations:^{
            CGRect postsFrame = self.PostsView.frame;
            postsFrame.origin.x = self.MenuView.frame.size.width;

            self.PostsView.frame = postsFrame;
        }];
    });
}

Where my code basically consists of a containing view controller that contains two child view controllers (the main view controller and the sidebar view controller) built in the Storyboard.

I've also tried setting View controller-based status bar appearance in the plist to NO and calling [[UIApplication sharedApplication] setStatusBarHidden] but the exact same result happens where it breaks.

What am I doing wrong?

回答1:

I'm not sure why that line breaks you animation, but it worked properly if I did what you said in your last paragraph, but added a layoutIfNeeded to the view before the animation (I did set View controller-based status bar appearance to NO in the info.plist).

- (void)viewDidAppear:(BOOL)animated {
    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.PostsView addSubview:[[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO]];
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
        [self.view layoutIfNeeded];
        hide = YES;
        [UIView animateWithDuration:1.0 animations:^{
            CGRect postsFrame = self.PostsView.frame;
            postsFrame.origin.x = self.MenuView.frame.size.width;

            self.PostsView.frame = postsFrame;
        }];
    });
}

Also it's unnecessary to implement prefersStatusBarHidden.