4

I am inside a UINavigationController. The UINavigationController has 3 view controllers. The first 2 are tableViewControllers, the last one is a regular view controller, with a PageViewController embedded.

I am using the following code in the third view controller in the stack to make the UINavigationBar clear:

navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)

I put this code in the third view controller in the stack. That is the view controller with a UIPageViewController embedded. But the nav bar is black. However, when I pop the third controller off the stack and go back to the second, a UITableViewController, the navigation bar there is clear.

I have looked at numerous other questions, here: How to make completely transparent navigation bar in iOS 7 and here: Transparent UINavigationBar in Swift but nothing works.

what am I doing wrong? the 2 lines of code above are the only ones I'm using, but I have also tried the suggestions in the links above, and nothing works. Here is a photo of the black nav bar:

enter image description here

Community
  • 1
  • 1
jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72
  • Can you show us the where in the life cycle of the view controller you are trying to manipulate the navigationBar, and how you are presenting the third view controller? It sounds like your code is working (the bar when you pop the 3rd VC off the stack is transparent) but it is happening too late. Have you tried putting the navigationBar code where the 3rd VC is being presented? – ackerman91 Mar 08 '16 at 05:50
  • you can just set `self.navigationController?.navigationBar.barTintColor = .clearColor()` instead of setting an image to it – iamalizade Mar 08 '16 at 06:05
  • I have tried it in 1) the viewDidLoad, 2) viewWilAppear, and 3) prepareForSegue in 2nd view controller. As I said, when I go back, the navigation bar in the 2nd VC is clear. But when I got back to the first one, it is black again. Here is a 45 sec youtube video showing the problem: https://www.youtube.com/watch?v=ubNBMmt2oLU – jjjjjjjj Mar 08 '16 at 06:06
  • @iamalizade I tried that and it did not affect anything – jjjjjjjj Mar 08 '16 at 06:11
  • @joey hmm, it should work. May be you overwrite it elsewhere? – iamalizade Mar 08 '16 at 06:13

1 Answers1

7

What worked for me, when I was struggling with the same problem, was to subclass the NavigationController, and use this in the viewDidLoad method:

self.navigationBar.translucent = YES;
self.navigationBar.shadowImage = [UIImage new];
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

another reason might be that your ViewController doesn't extended under your UINavigationBar, and thats why you see the black part, try setting this in the viewDidLoad of your ViewContorller

self.edgesForExtendedLayout = UIRectEdgeTop;
Eli Braginskiy
  • 2,867
  • 5
  • 31
  • 46