How to make UINavigationController NOT resize its view when setting the translucent property?

I have an application in which so far I have used the UINavigationController with the UINavigationBar, which has its property translucent = YES. This means that the presentation of the contents of the UINavigationController (i.e., the views from the view controllers) is called fullscreen (minus the status bar).

However, if you set navigationBar.translucent = NO, this kind of container will become 44pt shorter, as I suppose Apple suggested that you don't need content under the opaque navigation bar.

... unless you are doing what you are doing and using the navigation bar that scrolls (see this post on how to do this ). So I would like to know if this is possible.

I want to have translucent = NO, but everyone behaves as if he is still set to YES. I like the functionality of translucent = YES, but I really don't want the panel to be translucent using UIKit.

+8
ios uinavigationcontroller uinavigationbar
source share
4 answers

I found a solution that works, although it is really a bit hacked.

The idea is to give a translucent navigation bar an opaque base. Unfortunately, I am not happy with the decision that it is dirty and not encapsulated and presents some potential problems, but I am happy because it did its job.

In my view controller class of the base view (i.e. MyViewController: UIViewController) in the viewDidLoad method, I create a new ivar UIView * _navigationBarBG and give it the same frame as self.navigationController.navigationBar. Then I set the backgroundColor property to [UIColor whiteColor], although I suppose you have achieved even greater hue. [EDIT: If you want to be a purist (color values ​​remaining exactly the same as they come from .psd), you can do _navigationBarBG UIImageView and use your own background there, and the background of the actual UINavigationBar that you set for drawing (or stretch 1px transparent image, if you want to use the typical "change your navigation bar using an image", which is located somewhere on the Internet)]

if(self.navigationController) { _navigationBarBG = [[UIView alloc] initWithFrame: self.navigationController.navigationBar.frame]; _navigationBarBG.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_navigationBarBG]; } 

THEN (and this is the trashy part, but I don’t see another way), I add this view as a peep. BUT, whenever you usually make a call to [self.view addSubview: anyView], you need to make sure you call [self.view insertSubview: anyView belowSubview: _navigationBarBG];

 if (_navigationBarBG) [self.view insertSubview: anyView belowSubview:_navigationBarBG]; else [self.view addSubview: anyView]; 

If you forget this, these added views will glide beneath your navigation background and look weird. Therefore, you need to know that this is the source of errors.

WHY DO I DO IT? Again, you may ask ... I want to have a scrollable navigation bar that scrolls out of the way when you scroll down the table view, thereby giving the user more screen space. This is done using the scrollView delegate (scrollViewDidScroll :), as well as viewWillAppear:

  // FIRST DEAL WITH SCROLLING NAVIGATION BAR CALayer *layer = self.navigationController.navigationBar.layer; CGFloat contentOffsetY = scrollView.contentOffset.y; CGPoint newPosition; if (contentOffsetY > _scrollViewContentOffsetYThreshold && self.scrollingNavigationBarEnabled) { newPosition = CGPointMake(layer.position.x, 22 - MIN((contentOffsetY - _scrollViewContentOffsetYThreshold), 48.0)); // my nav bar BG image is 48.0 tall layer.position = newPosition; [_navigationBarBG setCenter: newPosition]; // if it nil, nothing happens } else { newPosition = kNavBarDefaultPosition; // ie CGPointMake(160, 22) -- portrait only layer.position = newPosition; [_navigationBarBG setCenter: newPosition]; // if it nil, nothing happens } 
0
source share

What worked for me is to add extendedLayoutIncludesOpaqueBars = true to viewDidLoad

something like that

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

Hope this works for you too.

+10
source share

This is not necessarily a good answer, but you can just compensate for your opinion, which is high if you are 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 can put this 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 a subclass.

+1
source share

I was looking for the answer to this question, because I wanted my routines to be at (0,0) and not (0,44) (regarding screen borders), but I could not find an answer on how to set this in the NavigationController which I thought would be included.

What I ended up with was very simple - add a subview to the navigation controller that was the width and height of the navigation bar, and then insert a preview under the navigation bar.

Now the setting is Translucent = YES, but it still seems solid, and the subzones behave the way I want.

EDIT: after re-reading your original post, I suggest that if you are going to reset the navigation bar, you will need to consider hiding and displaying the new subtitle as you do the same with the navigation bar

0
source share

All Articles