Is this possible?
I want to change the alpha value of the navigation bar in my view controller (in an animation), but if I do self.navigationController.navigationBar.alpha = 0.0;
, the portion of the screen the navigationBar took up totally disappears and leaves a black box, which is not what I'd like (I'd prefer it to be the color of self.view
's background).
Directly from the Apple Developer reference:
You can however set the translucence property of the navigation bar. If you do
[self.navigationController.navigationBar setTranslucent:YES];
should solve your problem. You can also try seeing if any of theUIBarStyle
enums are something you want.Swift3
If you just want to get rid of the
navigationBar
in an animated way you could do:If you want to control the animation and its necessary to set the
alpha
to0.0
, read on:The "black box" you are seeing is from the underlying view or window. If you just want your views color instead of the "black box" do:
If you want your actual view to be where the
navigationBar
was, you need to increase theheight
of your view:MickBraun's answer in Swift:
In AppDelegate.swift add these lines in didFinishLaunchingWithOptions:
Implement convenience methods to programmatically create UIImage objects.
You have a few options depending, in part, upon the barStyle of your
UINavigationBar
. The main thing is realizing that you likely don't necessarily have to animate the alpha property to get the effect you're describing.UIBarStyleDefault
orUIBarStyleBlackOpaque
Option A is to set your UINavigationBar translucent property to YES, then animate the alpha:In this scenario your view will be positioned behind your navbar, even when its alpha is 1.0. The downside to this scenario is that even with a 1.0 alpha you might see a tinge of your view's background color behind the UINavigationBar. Also, all of your subviews will need to be positioned 44 points down from the top.
UIBarStyleDefault
orUIBarStyleBlackOpaque
Option B is to hide the navbar in a cross-disolve transition animation. This will expose the superview of theUINavigationBar
. If you're using aUINavigationController
then the black background of theUINavigationController
view is what you'll see - but you can set the background color of theUINavigationController
view to match your view to get the effect that you want:One thing to watch out for with this solution might be a layout issue if the
UINavigationController
updates your view frame because you hid theUINavigationBar
. This would be fine except that your subviews might shift up 44 pixels if they're anchored to the top left. To work around this you might consider anchoring your subviews to the bottom of your view instead (either with springs or with layout constraints).UIBarStyleDefault
orUIBarStyleBlackOpaque
Option C is to cover up theUINavigationBar
with another view, again using a cross-disolve transition animation:UIBarStyleBlackTranslucent: This option is the easiest, as the
UINavigationBar
is already translucent, and your view is already behind it. Simply animate the alpha:This should get you the effect you desire:
Did not try this in an animation, works in my viewDidAppear though, hope it works.