How to fade in / out of navigationBar on iOS 9?

The built-in photo application disappears to / from the navigationBar when you click on the image. Thus, the "Photos" application allows you to see it in full screen.

How to do it (fade efect)?

As I understand it, navigationController?.navigationBar.alpha no longer working (so you cannot animate it).

+7
ios ios9 uinavigationbar
source share
1 answer

Share all my finds.

Complaint mode

Honestly, I feel half fraudulent / like a mannequin that I had to deal with a good day in order to realize the simple thing that exists in the Apple app.

Complaint Mode Off

First of all, this is some kind of context. I work with the navigationBar provided by the navigationController (compared to individual bars that are manually discarded in your view).

There are several approaches that I have found. I will tell them about all (even if I could not use them)

1) Animation change alpha navigation bar

 UIView.animateWithDuration(0.1, animations: { navigationController?.navigationBar.alpha = 0 }, completion: nil) 

@rmaddy mentions here that he works for him. However, I believe that it has a separate bar (against the bar controlled by the navigation controller).

I used the Reveal tool to check the user interface hierarchy and found a couple of things. - There is a hidden navigation bar (and navigationController?.navigationBar refers to it). Thus, you can change the alpha in your hearts, but these changes will not be visible.

There is, however, another navigationBar. I assume that it refers to some private members of the navigationController (let me call it private navigationBar). This is also visible at the top of your view.

2) Use setNavigationBarHidden: animated:

This is the standard way to hide / show the navigation bar. It animates differently (it slides / up and down). However, if this is normal for you, just go with it because it is simple and clean.

 navigationController?.setNavigationBarHidden(true, animated: true) 

Alternatively, you can wrap it in UIView.beginAnimations, UIView.commitAnimations to animate it along with some other materials (to make it smoother)

3) Animation of alpha changes of a private navigation bar.

This worked for me:

  let privateNavigationBar = self.superview?.superview?.superview?.superview?.superview?.superview?.subviews[1] UIView.animateWithDuration(0.1, animations: { privateNavigationBar.alpha = 0 }, completion: nil) 

I am going up the hierarchy to get a view that contains a private navigation bar (which is the second subquery for this view).

However, this approach has several disadvantages:

  • I believe the number of views? depends on your application hierarchy (in addition, you use a split view, etc.). I think you can generalize or maybe just go through the entire hierarchy to find the non-hidden UINavigationBar to solve this problem.
  • I have a feeling that Apple might frown on it (your application will not be accepted in the AppStore)

4) Make the navigation transparent and set the background image transparent and change the alpha channel on it.

I can not find where I read about this idea. There were a few references.

There is an example Apple application that shows how to configure the NavigationBar, including making it transparent.

Interestingly, this example application works for me (the navigation bar in it is transparent). However, when I tried this code in my application, it did not work (I still do not understand what is happening with this). As usual, there are a bunch of variables (maybe something in Info.plist, they are also subclasses of NavigationController, they can also be something like a hierarchy)

5) Adding Offline Navigation Bar

You can hide the panel provided by navigationController. Add your own in the UIView, connect it to @IBOutlet and use alpha animation on it (most likely what @rmaddy also pointed out).

I checked and this is work.

This approach is used in this tutorial.

However, it has the disadvantage of:

  • I believe that it will not rotate correctly, increase the height of the status panel during a call or GPS

Every time I see such code (written in an article), I know that there will be problems with resizing: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0)

You can replace it with restrictions. I went along this route, but came across some problems.

6) Other methods

I saw two more methods. I do not know if they will work or what will be the disadvantages:

One of them in this question: How to hide / show the status bar and navigation bar, fading in / out at the same time as the Photos application in iOS 7?

And this answer: fooobar.com/questions/352097 / ...

+5
source share

All Articles