How to hide / show the status bar and navigation bar while fading in / out at the same time as the Photos app in iOS 7?

I'm trying to hide and show the status bar and navigation bar, fading away at the same time and at the same time as the Photos application in iOS 7. I have a hiding part, but I have problems with the part shown. The problem is that when I show the navigation bar, it is initially positioned as if the status bar is missing there. At the end of the attenuation, it is positioned correctly (it is shifted down to make room for the status bar). How can I make the navigation bar fit correctly throughout the animation?

Here, some code will outline my current approach:

In a way, the controller, I control whether the status bar is hidden by overriding some UIViewController methods:

- (BOOL)prefersStatusBarHidden { return self.forcedStatusBarHidden; } - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { return UIStatusBarAnimationFade; } 

To hide the status bar and navigation bar at the same time, I perform both actions in the same animation block:

 void (^animations)() = ^() { theNavigationController.navigationBar.hidden = YES; someViewController.forcedStatusBarHidden = YES; [someViewController setNeedsStatusBarAppearanceUpdate]; }; [UIView transitionWithView:theNavigationController.navigationBar.superview duration:0.5 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:animations completion:nil]; 

(Note: I use theNavigationController.navigationBar.hidden = YES instead of [theNavigationController setNavigationBarHidden: YES animated: YES] because I want the navigation bar to disappear and not to slide In addition, for some reason not including the UIViewAnimationOptionAllowAnimatedent parameter matter.)

But if I do something similar to show the status bar and navigation bar together, I get the problem that I mentioned earlier.

 void (^animations)() = ^() { someViewController.forcedStatusBarHidden = NO; [someViewController setNeedsStatusBarAppearanceUpdate]; theNavigationController.navigationBar.hidden = NO; }; [UIView transitionWithView:theNavigationController.navigationBar.superview duration:0.5 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:animations completion:nil]; 

The closest thing I got to make it look right is to show the lines in sequence, and not in a single animation block:

 someViewController.forcedStatusBarHidden = NO; [someViewController setNeedsStatusBarAppearanceUpdate]; void (^animations)() = ^() { theNavigationController.navigationBar.hidden = NO; }; [UIView transitionWithView:theNavigationController.navigationBar.superview duration:0.5 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:animations completion:nil]; 

But now the bars do not disappear together. ( EDIT . If I put the first two lines in my own animation block to make the animation of the status bar fade out, I get the original problem with the navigation bar.) How to fix this

Note. I am using a custom background image for the navigation bar. If I just use the default frozen / blurred background for the navigation bar, another problem is that the background is invisible when it needs to fade and suddenly appears at the end of the fade animation. If I can make it work on a dull / blurry background, it will be great.

Another note: just in case it matters, the navigation controller is represented by theNavigationController.modalPresentationStyle = UIModalPresentationCustom .

+2
objective-c ios7 uinavigationcontroller uinavigationbar
source share
1 answer

I understood the answer to my question. The trick is to turn off the animation just to set the frame, borders, and center for the navigation bar during the fade animation. This is done by subclassing the UINavigationBar and conditionally using [UIView performWithoutAnimation ...] when the frames, frames and center are set. For example:

 - (void)setFrame:(CGRect)frame { if (self.shouldAnimateDimensions) { [super setFrame:frame]; } else { [UIView performWithoutAnimation:^{ [super setFrame:frame]; }]; } } 

Then the attenuation code becomes:

 void (^animations)() = ^() { // myNavigationBar is theNavigationController.navigationBar myNavigationBar.shouldAnimateDimensions = NO; someViewController.forcedStatusBarHidden = NO; [someViewController setNeedsStatusBarAppearanceUpdate]; myNavigationBar.shouldAnimateDimensions = YES; theNavigationController.navigationBar.hidden = NO; }; [UIView transitionWithView:theNavigationController.navigationBar.superview duration:0.5 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations:animations completion:nil]; 
+1
source share

All Articles