问题
I'm trying to create a twitter app, emulating the real app, using the twitter API. On the timeline view, I have a regular navigation bar, and when a user taps a user profile, the profile view has a transparent bar so that the user's banner image can be displayed. I used the following code to make the navigation bar transparent in the user profile view:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
The problem I'm having is when I return from the profile view, the timeline navigation bar has become messed up (screenshots below). I'm guessing this is because I changed some aspects of the navigation bar in the view controller for the profile view, and that carried over when I returned to the timeline view.
How can I reverse the navigation bar transparency to fix the wonky navigation bar? In the timeline view controller I've tried using the following code to try to reverse it, but it doesn't work.
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.isTranslucent = false
Timeline view with regular navigation bar
Profile view with transparent navigation bar
Timeline view with wonky navigation bar after returning from profile view
回答1:
A similar case happened to me too. Later, I noticed that I didn't change the attributes of the navigation item in the right life cycle metod. Make it non-transparent, in viewWillAppear
in timeline view controller and make it transparent before leave from the timeline view controller, in viewWillDisappear
.
Could it be related to that?
回答2:
In your viewcontroller in which you are changing navigation bar do something like
override func viewWillAppear(_ animated: Bool) {
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
navigationController?.view.backgroundColor = .clear
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.isTranslucent = false
navigationController?.view.backgroundColor = .blue
super.viewWillDisappear(animated)
}
回答3:
@Julian, I had the same issue as you. I had a master view with a red navigation bar and wanted the detail view navigation bar to be transparent. I managed to get that working, but encountered the same glitch when going back to the master view. The navigation bar stayed transparent for 1.5 seconds or so, and then went back to the original red color. I couldn't solve it with styling the navigation bar, but managed to fix it a different way. It's not a preferred solution, but it has cost me so many hours now, that I'm okay with it.
Alright, what I did, was create an ImageView, and aligned that under the navigation bar with auto-layout. The navigation bar was 44 pixels in height, so the ImageView got the y position -44. I gave the ImageView the red color of the navigation bar, so that when it was transparent for 1.5 seconds, the user actually saw the ImageView instead of the navigation bar color.
I hope this helps you too.
来源:https://stackoverflow.com/questions/42332547/swift-3-how-to-reverse-a-transparent-navigation-bar