Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to make nav bar clear, and it's black

Tags:

ios

swift

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

like image 243
jjjjjjjj Avatar asked Mar 13 '23 00:03

jjjjjjjj


1 Answers

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;
like image 52
Eli Braginskiy Avatar answered Mar 25 '23 03:03

Eli Braginskiy