barButtonItem on tabBarController - ios

i am wanting to add a barButtonItem onto a tabBar so i don't have to use a navigation controller at the top of the screen.
My ViewControllers are embedded in a tabBarController:
And i want to add a barButton on each viewcontroller e.g.:
and them implement some code like:
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 4 {
}
}
to run a function when this button is tapped.
problem is this button isn't showing on the tabBar when the app is built.
i was hoping this would achievable without having to create a custom tabBar.

I don't quite understand what the problem you're describing is. Are you trying to add a 'Logout' button at the top right of your screen by only using a UITabBarController? Why are you against using a UINavigationController? It's incredibly easy to implement using storyboards (which it looks like you're using anyway).
Apple even has a page on how to implement a UINavigationController within a UITabBarController here: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html
Key information here:
To create a combined interface with three tabs that contain custom view controllers and one tab that contains a navigation controller:
Create three custom view controllers, one for each tab, and a navigation controller.
Select the three custom view controllers and the navigation controller (only the navigation controller scene, not it’s root view
controller).
Choose Editor > Embed In > Tab Bar Controller.
Display the tab bar controller as the first view controller by selecting the option Is Initial View Controller in the Attributes
inspector (or present the view controller in your user interface in
another way.)

According to Apple Documentation,
UIBarButtonItem - A bar button item is a button specialized for
placement on a UIToolbar or UINavigationBar object. It inherits basic
button behavior from its abstract superclass, UIBarItem. The
UIBarButtonItem defines additional initialization methods and
properties for use on toolbars and navigation bars.
Since you're against putting a Navigation Controller in your project, a UINavigationBar is not implemented which is what UIBarButtonItems are for. If you want to implement your Menu Button on top of your screen in one of your view controllers. Why not just use a UIButton as inside a UIView like this?? The blue background is just a regular UIView with UIButtons inside of it.

Related

Different navigation item for each view in tab bar controller

The Problem
I'm relatively new to Swift and I'm trying to build an application that makes use of a UITabBarController.
What I'm trying to do is put a different navigation bar (or UINavigationItem) on each of the tabs in the UITabBarController.
For example, I want the UINavigationItem I set, with its bar button items, to appear on MyViewController instead of a back button to the previous view controller, such as shown on the image below.
The current layout on the Storyboard is as follows.
MyViewController on the sidebar:
What I've Tried
Someone suggested that I should embed each UIViewController (e.g. MyViewController) in a Navigation Controller. I've tried this and it doesn't work.
I've also tried to set the Top Bar to "None" in the Attributes tab of the options menu.
Thank you in advance for your help.
Here is how i did it,
UINavigationController -> UITabbarController
And then each "Tab" is in different Storyboard and every storyboard start with a "Navigation Controller". So yes every tab in different navigation controller this how you should do it.
Different storyboards because may be multiple people work on storyboard at same time.
Why TabbarController inside Navigationcontroller ?
I put the "TabbarController" inside "NavigationController" because some of the controllers i want them to be full screen, like hiding the "Tabbar" so for that i push them from main NavigationController.
//Out of context but may help you,
I have created an "Extension" of Navigation Controller to push a view controller on main navigation so that any of the tabbars (which are also inside navigation controllers) can easily use the extension to push any view controller if want to hide the tabbar.

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.

Implement a bar over ViewController that does not animate (like tab bar)

I have two main ViewControllers in my project. I would like to add in both ViewControllers a bottom bar that has buttons (different for each ViewController) that take user to a respective subViewController, just like a Tab Bar. But the first ViewController must have a button inside its View that takes the user to the other main ViewController without animate the bar during the transition among ViewControllers. How can I do this?
I suppose you could add the view as a subview of UIWindow, and not your respective view controller. Alternatively, why not subclass UITabBar?

UIBarButtonItem added to storyboard does not appear at runtime

When I try to add a UIBarButtonItem to my UIViewController, nothing shows up at run time.
What I did:
I started off with a brand new single view project.
I dragged a UIBarButtonItem into the view controller.
I can now see and customize the item in the storyboard, but when I run it, there is no toolbar.
Note: the Bar Button Item appears as a direct child of View Controller.
How can I get the UIToolbar to appear?
Adding toolbar items as direct children of the UIViewController corresponds to the toolbarItems property of a view controller. The documentation states:
If this view controller is embedded inside a navigation controller
interface, and the navigation controller displays a toolbar, this
property identifies the items to display in that toolbar.
So, you must do the following:
Embed the UIViewController in a UINavigationController (e.g. select the UIViewController in the storyboard, choose Editor > Embed In > Navigation Controller).
Select the UINavigationController and check the box that says Shows Toolbar (this is the similar to calling self.navigationController.toolbarHidden = NO in the view did load method).
Optionally, if you want to restore the behavior where the navigation bar wasn't visible, then uncheck the Shows Navigation Bar property.
An alternative approach is to not use the toolbarItems property, and instead add your own toolbar and maintain it yourself (e.g. add an IBOutlet and interact with it that way).

Is there a way to subclass UIToolbar?

I ask this as I would like a global toolbar in my app (similar to the Facebook app with buttons on it). How do I subclass it so that I can add it on all of my ViewControllers? I've created a new class but UIToolbar isn't in the options. How would I modify NSObject manually?
Edit:
My controller hierarchy is as follows:
A login page, which pushes (flip horizontal) -----> Tab Bar Controller. How would I add this toolbar to the top of each page in the tab bar controller?
If you are using a UINavigationController as your window's root view controller, you can let the navigation controller display a toolbar. You need to tell it to show the toolbar by sending it a setToolbarHidden:animated: message. If you want the toolbar to have the same buttons (or other subviews) in all your view controllers, make sure you set each view controller's toolbarItems property to the same array of items.
If you are not using a UINavigationController as your window's root view controller, edit your question and describe your view controller hierarchy.

Resources