How to hide navigation bar on scroll except when at the top of the screen - ios

I have a UIViewController that contains a UICollectionView pinned to all edges of the view. This view controller is inside a UINavigationController.
I want to gradually hide the navigation bar as I scroll down in the collection view. At the point that I have scrolled the distance of the height of the nav bar, the nav bar should be completely hidden. If I scroll back up it should gradually show the nav bar.
I have tried all the open source navigation bars on github, but none of them work correctly with iOS 12.
How can I achieve this?

UICollectionView is a subclass of UIScrollView and therefore you have access to its scrollViewDidScroll delegate method. Your UIViewController is also owned by its navigation controller, so you can create an instance property in the view controller, like navigationDelegate: UINavigationController?, that will act as a delegate. In the navigation controller, set that property equal to self and manipulate the nav bar however you want through the scroll delegate. Absolutely no need for third-party scripting for something this standard and basic.

Related

Change location of Navigation Bar

When using the Navigation Controller it creates a new Navigation Bar on my first view controller. Intead of that bar being at the top of my screen I would like it to be on bottom. Is this possible? If not can I switch to using a different Nav Bar?
Navigation Bars are always at the top of view controllers, and cannot be moved. You could explore a TabBar or ToolBar though, depending on what you want.
Navigation Bar always appear on the top of your UIViewController when embedded in a UINavigationController. You cannot move it to any other position.
Still if you want to achieve such a requirement,
Hide the default UINavigationBar
self.navigationController?.navigationBar.isHidden = true
Create a custom UIView of same height(44) as the UINavigationBar and pin it to the bottom of your controller.

Adjust Navigation Bar width inside NavigationController

I am trying to implement a horizontal scrolling table view. I was able to accomplish this with the following layout. I have a ScrollView which contains a Container View which points to my NavigationController. Since I made the Container View wider than the ScrollView, the table is able to scroll left to right as expected.
The problem I am having is resizing the Navigation Bar's width as I do not want to have to scroll to view the Navigation Bar's title. I tried explicitly setting the width property of the Navigation Bar in ViewDidAppear however it keeps getting resized to the actual width of the table view. Is there any way I can do this without having to create my own custom view that mimics that Navigation Bar?
I ended up creating my own navigation bar that is placed on top of the actual integrated table view navigation bar.

Can I place a UIView the current Navigation Controller?

How can I place a UIView overtop of a Navigation Controller? Is this possible without making my own custom navigation bar and custom tab bar controller out of UIViews?
Let's say I have a UITabBarController and I want to present a blur view overtop of the entire thing. For the blur view, I'm using the UIVisualEffectView. But i want the blurview to take up the entire screen including the top navigation bar and the bottom tab bar. If I push a new view controller, that will take up the entire screen, but then I can't see through it to what's underneath (the tab bar controller's contents).
I could simply hide the navigation bar and tab bar when I animate in the blur view, but that looks awkward because you see the content in the collectionview shift because the navigation bar is hiding... I'd rather not see that shift in the content when the blurview comes up.
Here's a UITabBarController with stuff in it.
I want a blur view to cover everything and to be able to see through to the entire UITabBarController and the stuff underneath.
One way is to add the UIView as a subview of the key window. See this post for how to get the key window. However, it's generally best to keep to your own view hierarchy, so...
Another way might be to present a new view controller as a fullscreen modal, and apply the blur/transparency to that modal view.

setting navigationcontroller bar to opaque moves detail scroll view down

I am using a search bar plus controller for a UITableview in my MasterController. When the view shows up, the navigationbar in the MasterController is black until the view fully loads (not sure why?). In order to fix this I set the self.navigationController.navigationBar.translucent = false; This fixed the issue I was having with the navigation bar being black initially, but now it looks like when I click a cell in the uitableview of the MasterController, the content in the DetailView that loads shifts down approximately the size of the search bar, but it's just blank space above the content that has been shifted... When I remove the translucent property in the MasterController the content in the DetailView is fine and not shifted...
SO my question is, what is shifting this content down and how can I stop the content from being shifted AND having an opaque navigation bar...
In iOS 7, by default your view controller's view extends under a translucent navigation bar when contained in a UINavigationController but not on an opaque one. You could set the extendedLayoutIncludesOpaqueBars property of the view controller if you want it to extend under opaque nav bars too.
When you say your scroll view is shifted on your detail view controller it's probably because its y origin is not set to zero to offset for your view extending under a translucent nav bar, but because your nav bar is now opaque and your view is not extending under it the space is now visible.
One thing more view controllers will actually automatically add insets on scroll views they contain so that their contents are not obscured when the view controllers view extends under navigation bars. It is set by the automaticallyAdjustsScrollViewInsets property which is by default is set to YES.

In iOS 7, why UITableView's contentInset has bottom value despite the UITabBarController is hidden?

I don't know why UITableView has bottom inset automatically despite I make UITabBarController be hidden by calling [setHidden:YES] before.
The view controller who has UITableView is a child of UITabBarController. I already know that automaticallyAdjustsScrollViewInsets helps any UIScrollView get proper 'contentInset' depending on status of it's container view controller.
So, I expected that UITableView's bottom contentInset will be 0 if UITabBar is hidden. But, doesn't do that.
Although automaticallyAdjustsScrollViewInsets is YES, should I manually adjust that value when UITabBar is hidden?
Tab bars have never been meant to be hidden - after all why have a UITabBarController if you want to hide the tab bar. In the documentation, you are warned not to modify the tab bar object directly:
You should never attempt to manipulate the UITabBar object itself
stored in this property.
This is exactly what you are doing when setting it to hidden.
In iOS6 this has worked, but now in iOS7, it doesn't. And it seems very error prone to hide it. When you finally manage to hide it, if the app goes to the background and returns, Apple's layout logic overrides your changes. There are many such triggers.
My suggestion is to change your design. Perhaps display the data modally.
Putting this here for anyone who gets this problem for nested view controllers.
My view controller containment hierarchy is:
UINavigationController
|--UIViewController
|--UITabBarController
|--UIViewController
The last view controller has a UITableView whose scrollIndicatorInsets keep getting offset by the tab bar controller's UITabBar height even if it is hidden.
Solution: Set automaticallyAdjustsScrollViewInsets to false in the view controller that contains the tab bar controller (and is inside the UINavigationController). No need to set additional properties in the tab bar controller itself and the second view controller where the UITableView is.

Resources