Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a UINavigationController to NOT change its view size when setting the translucent property?

I have an app where up until now I've been using a UINavigationController with a UINavigationBar that has its property translucent = YES. This means the UINavigationController's content view (i.e. the views from the view controllers you push) to be full-screen (minus status bar).

However, if you set the navigationBar.translucent = NO, this container view becomes 44pt shorter, as I suppose Apple has assumed you don't need any content under an opaque navigationBar.

... except if you're doing what we're doing and are employing a navigationBar that scrolls away (see This Post on how to do that) So I'd like to know if this is possible.

I want to have translucent = NO, but have everything behave as if it were still set to YES. I like the functionality of the translucent = YES, but I don't actually want the bar to be made translucent by UIKit.

like image 761
horseshoe7 Avatar asked Nov 05 '12 15:11

horseshoe7


People also ask

What is translucent navigation bar?

Translucent or partially transparent is when the colour can be seen but the background behind the object is also slightly visible through it. For this we use. background-color: #FFFFFF; opacity: 0.5; filter: alpha(opacity=50);

Can you change the navigation bar on Iphone?

Change the Bar StyleA user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.


2 Answers

What worked for me was to add extendedLayoutIncludesOpaqueBars = true in viewDidLoad

something like this

override func viewDidLoad() {
   super.viewDidLoad()
   extendedLayoutIncludesOpaqueBars = true
}

Hope it will work for you as well

like image 196
chrs Avatar answered Oct 19 '22 22:10

chrs


It's not necessarily a good answer but you could just offset your view that high if you're not translucent.

//This won't take into account orientation and probably other details
if(!self.navigationController.navigationBar.isTranslucent)
{
    self.view.frame = CGRectMake(0,0,-44,self.view.bounds.size.height);
}

You could put that in your viewDidLoad or viewWillAppear and if you have a bunch of view controllers you can just subclass them all and put your logic in the subclass.

like image 39
rooster117 Avatar answered Oct 19 '22 22:10

rooster117