How do i connect UITabBar items to view controllers in IB - ios

I want to connect UITabbar items to different view controllers. I dont want to use UITabBarController. I have a UITabBar in a ViewController. I am aware of the delegate methods also. Just wanted to know if there is a way to do this through the interface builder(Ctrl+Drag way). Ctrl+Drag only works for elements in UITabBarController and not for UITabbar.
PS: Same question has been asked before but re-posting because there is no satisfactory answer for this.
How do I connect UITabBar items from a UITabBar (not a UITabBarController) to different views in the IB?

I am really sorry. But the fact is that you can't.
UITabBar is a subclass of UIView. Better use UITabBarController . It will provide you a base controller class and works as a container for rest of your controllers.

Related

Easiest way to add upper Navigation bar and "back" buttons to UIViewController?

In my current storyboard file, there are lots of (about 150) view controllers that are UIViewController instances connected with segues, and now my client requested me to add a Navigation bar and "Back" button to every existing view controllers.
I found out that "just simply adding UINavigationBar to UIViewControllers doesn't make results like the most views found in famous apps. In fact, the famous views seem to be built by UINavigationController, not UIViewController. So, probably replacing UIViewController to UINavigationController will solve.
But since there are 150 of them, it's a little too painful for me to replace all UIViewControllers to UINavigationController manually. I should have built my storyboard file with UINavigationController from the first time, but it's too late.
Is it sure it's the only way? Aren't there any other ways, or some kind of hack I can use to save my time?
I haven't seen your code so I'm just probing the situation but I think your best bet would be to wrap your first ViewController in a NavigationController.
To do this easily, select your first view controller in your Storyboard and select: Menu -> Editor -> Embed In -> Navigation Controller.
All the descendants of that original ViewController should inherit the embedding.

How to use same UINavigationBar for all UIViewControllers

In my app, I have to show same NavigationBar for all UIViewControllers. This NavigationBar has three buttons and these three buttons which will be act as TabBar functionality, that is each tab has its own stack cycle. I have created custom view for NavigationBar with three buttons, but after adding this custom view to HomeViewController, I have to manually add this custom view for all other view controllers. I don't want to do this.
Is there any simple method to achieve this?
There are a couple of ideas that come to mind. First of all, you could use view controller containment and actually have 1 controller that implements your custom nav bar, and then swap out the contained controller as necessary.
If that's not feasible, you can simply use inheritance and have all your custom controllers inherit from a controller that has the nav bar in place.
Another option could be to write your own UINavigationController subclass. I'm not certain if you can override the UNavigationItem behavior, but if you can, you can just just do that -- instead of the UINavigationController taking its child's UINavigationItem to update its own UINavigationBar, the UINavigationBar perhaps just stays the same, like you're expecting/hoping.

How to integrate custom view with TabBar?

Well I wish there was some tutorial anywhere or any of the O'Reily Cookbooks would explain this but everyone only ever talks about using UITabBarController.
What I need in my app however is a custom view (basic UIViewController) with a NavigationBar (just for the title bar and a 'Done' button) and a TabBar in it.
The question is: How do I integrate the TabBar and connect views to each bar button (and add more bar buttons)? Can this only be done programmatically or is it possible in Storyboard? And which class needs to be the TabBarDelegate (I suppose my custom UIViewController)?
Does anyone know of a good guide for this or provide me with some hints?
Thanks.
UPDATE:
I decided to simply check in the TabBarDelegate for the tabBar.selectedItem.tag and instantiate a sub view accordingly from the storyboard and add it into a ContainerView that is sandwiched between my nav bar and the tab bar at the bottom. This works so far (although I'm not sure if there's not a better approach) but now I'm facing a different problem:
When a sub view is loaded into the ContainerView the whole tab bar disappears. Does somebody know why this happens?
It is very easy to crate Tab Bar Controller app with Storyboard. Apple even provides a snippet :) When creating a new project, just select iOS -> Application -> Tabbed Application.
Also, here is a pretty good tutorial: https://www.youtube.com/watch?v=RhsHtd6rAiQ.
Solved the original issue myself by creating a Xib with UIViewController in it and place a UITabBar and a UIView as container for subviews. Then load the xib with NSBundle.mainBundle().loadNibNamed() and create Xibs for the sub views that should be loaded and load these with loadNibNamed() and add them with viewContainer.addSubview().

How to change view with UITabBar (without using UITabBarController or addSubview)?

I've tried a lot to get this done.I don't want want to use addsubview or uitabbarcontroller. I want to switch the view when I tap on different tabs and tabbar should stay there. Thanks a lot in advance.I m a newbie.I want to use uitabbar not uitabbarcontroller.
A simple approach would be to wrap the view controllers you are inserting in UITabBarController in a UINavigationController then you can simply push your new view to that UINavigationController and the tab bar would be always visible!
For more details refer Apple Docs
Edit:
If you don't want to use UITabBarController then you need to do 2 things:
Add the tab bar View to UIWindow that way it would be always visible.
Scale/adjust the views in your view controllers to leave space for the tab bar

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