UIBarButtonItem added to storyboard does not appear at runtime - ios

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).

Related

barButtonItem on tabBarController

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.

Using storyboard references with UINavigationController

I have the following hierarchy in my app: UITabBarController to many UINavigationControllers. Each navigation controller has a UIViewController.
I’ve split this up so that each UITabBarController’s child view controller is linked via a storyboard reference. In each of these references there’s a UINavigationController as the initial view controller.
Again some of these other storyboard references are split up too, where one of the UINavigationController’s child view controllers also uses a storyboard reference.
These UINavigationController’s child view controllers in the new storyboards do not carry across the UINavigationController style. Unlike the UITabBarController which does take across the UITabBarItem and shows them in the UINavigationController.
Is there anything I can do to see the navigation item of these UINavigationControllers so I can add bar button items in IB?
It's straight forward really; just go to Editor > Embed In > Navigation Controller and embed your view controller in a UINavigationController. You can then select the UIBarButton items under the Object Library in xCode, drag and drop them to the navigation bar on your View Controller and voila!
You can then delete the UINavigationController when done with adding the UIBarButton items to your VC; those bar button items will still be referenced and shown in the final product when you run it.
In Storyboard go to the Attributes Inspector for the Controller you wish to add the navigation bar and click on top bar drop down instead of it saying 'inferred' click on 'Translucent Navigation Bar'

Add UIBarButtonItem in Interface Builder to navigated UIViewController?

I have a UINavigationController in Interface Builder in a storyboard. I've added two UIBarButtonItems to the first UIViewController. They display just fine. How can I add them to the second UIViewController which is navigatated to? On that view controller, I only see the Back button.
When I try dragging a UIBarButtonItem to the top of the second view controller, the button ends up at the bottom of the scene for some reason. It is not displayed when running the app.
Edit: I should have mentioned that I know how to do this in code, but not in Interface Builder.
To add further informations about my comments concerning navigation item :
Here is a simple project with a UIViewController embedded in a UINavigationController. A second UIViewControllercan be pushed via the Next button.
If you select the next button, you can see in the hierarchy that this button is embedded within the group Right Bar Buttons Items, which is embedded within the First object. In fact, First is a UINavigationItem automatically added to your view controller when it was embedded in the navigation controller.
But the second view controller (the pushed one) doesn't have this navigation item since it isn't directly embedded within a navigation controller. Simply drag and drop it from the Object Library to your view controller.
#Slack, As i said earlier just drag and drop 2 "bar button item" in your navigation bar.

Set position of UIToolbar in Interface Builder

I'm having trouble getting a UIToolbar to appear at the bottom of the screen when my app loads. Regardless of how I position the UIToolbar and regardless of the constraints that I have put on the bar, it always appears in the middle of the screen. I have my navigation bar situated correctly, and I've never had trouble situating UI elements in IB before (although I'm new to iOS programming). Any ideas on how to make sure the toolbar stays in place? Thanks.
You're probably not playing the game correctly with UINavigationController. See:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/
Displaying a Navigation Toolbar
A navigation controller object manages
an optional toolbar in its view hierarchy. When displayed, this
toolbar obtains its current set of items from the toolbarItems
property of the active view controller. When the active view
controller changes, the navigation controller updates the toolbar
items to match the new view controller, animating the new items into
position when appropriate.
The navigation toolbar is hidden by default but you can show it for
your navigation interface by calling the setToolbarHidden:animated:
method of your navigation controller object. If not all of your view
controllers support toolbar items, your delegate object can call this
method to toggle the visibility of the toolbar during subsequent push
and pop operations. To use a custom UIToolbar subclass, use the
initWithNavigationBarClass:toolbarClass: method to initialize the
navigation controller.
EDIT to include answer to question in comments:
No code needs to be written to do all this. You can set "Shows Toolbar" in the Attributes Inspector of your Navigation VC. You can then drag individual UIBarButtonItems onto particular VCs (on which a toolbar will suddenly appear) and do the usual outlets and actions with them.

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