Custom NavigationBar height with button and label - ios

I'm trying to create a UINavigationBar that looks like this:
This is what I've managed to achieve...
My first idea was to try creating a back button on the UINavigationBar and just removing the line between the first header of the UITableView. Problem with that is, the UITableView header is not sticky. Can someone help me out with this?
I'm using Swift 4

My solution was this: Create a new UIViewController and put a View at the top of it as my header then use a UITableView as opposed to a UITableViewController.

You should be able to achieve that with the prefersLargeTitles setting on UINavigationBar available from iOS 11. If you're using a Storyboard, select the navigation bar, and check the "Prefers Large Titles" box.
In code, you can set the navigation bar's prefersLargeTitles property to true.
When you show a detail view controller, you may want to have more control over whether it uses the large title or not. You can do this on the view controller.
Easiest way to do that is in code. Something like this:
override func viewDidLoad() {
super.viewDidLoad()
// Disable large titles for this view controller
navigationItem.largeTitleDisplayMode = .never // Options are automatic, always, or never.
}

Related

how do I hide footer of all view controller witch attach with my navigation controller in swift project?

I am working on swift project, which uses Xcode 9 and swift 4.1. I created one navigation viewcontroller and then created other view controller and attached with that that navigation viewcontroller.
so I got header and footer by default.so I have a two problems,
Now I want a change a color of footer other than white but when I write any code for that It diminished behind footer and I don't see any color without white.How do I give any color to footer?
If I am not able to give color to footer so I want to hide that footer using bellow code
override func viewWillAppear(animated: Bool)
{
self.navigationController?.navigationBarHidden = true
}
But by this color I am able to hide only header not a footer. so how do I hide footer using code so I created customise footer what I want.
How do I solve above problem in swift 4.1?
I don't know what you mean by footer here because by default when you embed your viewcontroller inside navigation controller it doesn't show any footer.
There are two possible things that you are doing there:
Either your navigation controller is embedded inside TabBarController
Or you have unhidden the toolbar of navigation controller.
Now if you are using TabBarController as a parent of your navigation controller you can simply use following code to hide the footer:
self.tabBarController?.tabBar.isHidden = true
If you are using navigation controller's toolbar (which is hidden by default). You can use following code for hiding it:
navigationController?.setToolbarHidden(true, animated: false)
and for changing the color of toolbar you can simply use:
navigationController?.toolbar.barTintColor = .black

How to add refresh control to collection view large titles navigation bar in iOS 11?

According to Apple the refresh control should be part of the large title navigation bar in iOS 11.
The refresh control is part of the navigation bar (on pull to refresh) when I enabled the refresh control in my storyboard for a UITableViewController.
I can not do this in storyboard for all other views like UICollectionViewController. When I add a refresh control in code as a subview it is not part of the navigation bar:
refreshControl = UIRefreshControl()
collectionView?.addSubview(refreshControl)
It looks like this though:
How can I add a refresh control to my custom scroll view like UICollectionViewController in such a way that the refresh control is displayed in the navigation bar when large titles is used?
As of iOS 10, UITableView and UICollectionView has refreshControl property.
So, instead of:
tableView.addSubview(refreshControl)
you do:
tableView.refreshControl = refreshControl
and this should work for new big headers in iOS 11.
EDIT: The documentation has been updated at some point, and below information is not longer true.
As specified by Apple in UIRefreshControl documentation.
Note
Because the refresh control is specifically designed for use in a table view that's managed by a table view controller, using it in a different context can result in undefined behavior.
If your VC is a UITableViewController it will work exactly like in system apps.
In my case, I wanted to have UIView under UITableView, so UITableView wasn't first subview of view controller's view.
To fix this, I changed order of UITableView and other UIView
from
to
Now I just set zPosition of my view to lower value than UITableView's zPosition which should make my view look like it is "under" UITableView
myView.layer.zPosition = -1

Keep a header view when using a navigation controller

I have a label I placed at the top of the screen as a header. I want to keep that viewable and still present when I add a navigation controller by embedding it.
I am mostly using the storyboard to help me create the UI.
This is what the app looks like without a navigation controller:
However, if I add a navigation controller I get the following:
Certainly I would like to maintain a navigation controller so that I won't have to incorporate my own buttons which control navigation. As I understand it, at this time the Marketplace logo is hidden behind the nav controller.
You can use two methods here :
1) Keep using your label, and hide the navigation bar , your app will still have navigation, just the top navigation bar will be hidden.
For this, use this code:
viewDidLoad()
{
self.navigationController?.navigationBar.isHidden = true
}
2) The second method is a alternate to what you want, but its good if you Need text At top only. In this we will not use label, instead we'll set title of the navigation bar itself.
viewDidLoad()
{
self.navigationItem.title = "MarketPlace"
}
Maybe there are more methods, but these two i've used.
You can try navigationItem.titleView = yourView
In Objective-C it would be something like this:
navigationController.navigationBar.hidden = YES;

Swift: Add segmented controller to a navigation bar, and keep the title + nav buttons

I'd like to add a segmented controller to a navigation bar, below the title.
At the moment, I've got a storyboard UITableViewController with a CoreData-sourced dynamic table, embedded in a UINavigationController. I'd like the UISegmentedController to differentiate the sorting of the table.
There seems to be obj-C solutions available stackoverflow.com/questions/29480433/ and stackoverflow.com/questions/18813563, but I'm looking for a Swift solution, that resembles the native apps. I've also tried putting the segmented controller into a table cell with little success (can't get the action recognised). I'd prefer not to abandon the table view controller.
Any suggestions?
I replaced the UITableViewController with a UIViewController and UITableView, then added the UISegmentedController to a UIToolBar positioned below the UINavigationBar.
I used removeShadow for the navbar, then clipsToBounds for the toolbar. I also added another View to simulate the bottom shadow on the toolbar (because I couldn't get the toolbar shadow/background position function to work).

UINavigationBar inside UITableViewController

I have some static cells that I want to display, so I have a UITableViewController. There is also a NavigationBar in this scene that contains some buttons at the top. The setup looks like this:
If I had a UIViewController that contained a UITableView in it, the setup would look like:
So, the question is:
Why does the Navigation Bar have to be embedded inside the UITableView when using a UITableViewController? (I have tried putting it elsewhere but IB won't let me)
I know that UITableView is a subclass of UIView, but is it OK that the top level element in the hierarchy is not a View (but a TableView)?
Thanks.
You shouldn't be placing your UINavigationBar in your UITableView. You should be putting your UITableViewController in a UINavigationController, because that will provide a UINavigationBar for you.
So if you select your UITableViewController in the storyboard, you can choose Embed In -> Navigation Controller from the Editor menu. This would be the proper way to do it.
There are two ways to use a UINavigationBar in iOS:
Embedded inside a UINavigationController (recommended)
As a standalone object
For your particular situation, I'd recommend that you put your UITableViewController as the rootViewController of a UINavigationController. That way you automatically get a navigation bar which you can customize according to your needs. In a typical user experience, when you tap some of your table view rows a new view controller will be pushed onto the navigation stack, so you'll probably end up needing a navigation controller anyway.
What if you decide to use a navigation bar as a standalone object? This is perfectly fine, you can use it inside a view hierarchy as an ordinary UIView, but you'll need to create another object that implements the UINavigationBarDelegate protocol and set it as the delegate property of your navigation bar. If you use a UINavigationController the delegate is already set and configured for you. You also need to add/remove navigation items (instances of UINavigationItem) to your navigation bar by using the pushNavigationItem:animated: and popNavigationItemAnimated: methods.
And about your question on the view hierarchy, you can use a UITableView anywhere a UIView is required. The only caveat is that a UITableView is a view hierarchy on its own and that may restrict your layout a little bit.
The way a UITableViewController works, is its root view is a UITableView. So there is no way to put the UINavigationBar anywhere other than in the UITableView.
I tend never to use a UITableViewController as it doesn't really give you much.
If you particularly want to use the UITableViewController, I don't believe that there is any real problem in having the navigation bar within the table view. You just need to make sure that you set the contentInset on the table view such that the navigation bar doesn't block the content. Though it seems a bit backward to do it this way.
My recommendation would be to just use a normal UIViewController with a navigation bar and a table view.
If you actually need functional navigation, you need to put your UITableViewController within a UINavigationController.
Hope this helps :)
Let me know if anything is still unclear.

Resources