Set position of UIToolbar in Interface Builder - ios

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.

Related

UINavigationBar without UINavigationControler Swift

I want to add a navigation bar to my view controller (I do not use a navigation controller) to show the page title and contain an add button. However when adding a navigation bar though the interface builder it is not showing. I am assuming it is possible to use it without a navigation controller otherwise there would be little point in allow me to drag it on to a view controller.

Navigation Bar on Modal View Controller

I have a view controller that executed by a modal view (no Navigation Controller connected). I want to have a Navigation Bar on top and be able to change the buttons and title dynamically depending on what the user has selected on the view controller.
I have read that you can set the View Controllers Simulated Metrics to include either a Translucent or Opaque Navigation Bar.
However, after I select this and add a navigation item to it, I can't get it to show the buttons or title and also don't know how to reference it in code.
Can anyone help walk me through it?
Simulated metrics are a design aid to help in laying out your views; the navigation bar you add that way isn't actually added to your controller at run time. You need to drag out a navigation bar from the object list, and add it to your controller's view.
Simulated Metrics is only to simulate, nothing is changed on your app. If you need a Navigation Bar create it programatically or drag out a navigation bar from the object list.

How to add buttons to navigation controller visible after segueing?

I have set up a view controller in Storyboard which is embedded in a navigation controller. In this nav controller I have checked Shows Navigation Bar and Shows Toolbar and enabled a navigation bar and toolbar in Top Bar and Bottom Bar respectively in Simulated Metrics. I then "Show e.g. Push" segue from the view controller to another view controller. On both view controllers I've set up Simulated Metrics the same way, so both the navigation bar and toolbar are visible on all three controllers.
The problem is, I can add bar button items to the first view controller to both the navigation bar and the toolbar, but I cannot add a bar button item to the navigation bar on the second view controller. When I drop a button on the navigation bar, it's added to the toolbar instead. And I cannot double click the nav bar to add a title. In the outline I see there is no navigation item on the second view controller, but it is there in the first view controller.
I can probably add buttons to the navigation bar programmatically, but I want to do this visually in Storyboard. My question is, what is wrong with this setup, or is this a bug with Xcode?
For XCode 6, the UINavigationItem for the 2nd view controller onwards is not added automatically on the View Controller Object inside the storyboard. You will have to drag the UINavigationItem onto the Navigation Bar for that view Controller Object before adding UIBarButtonItem on top of it.
I am not sure why it is designed that way. I only discovered about this a few weeks ago.
If you have a ton of view controllers and very little custom auto-layout stuff in place, you can disable size classes, then re-enable it will add all missing nav bars across your storyboard.
Obviously not recommended if you have a ton of custom auto layout stuff linked up.
If you have just a single UIViewController that's missing a nav bar, just drop a UINavigationItem onto it.
If you're using Xcode 7 beta 4/5, try restarting Xcode it solved the issue for me
I just bumped into this issue and it seems to be affected by the segue that shows the view controller.
If "Kind" setting in segue is "Show (e.g. Push)", it's not possible to drag the item to top right corner. However, if you explicitly change "Kind" setting to "Push", this can be done.
Edit: actually I just noticed that "Push" is deprecated. Not sure what Apple is thinking here. However, you can just change it back to "Show" after adding the button and it seems to work :D
For second view controller in hierarchy, you can setup title in attributes, without adding "navigation item"
I think it is a bug. I had the same problem.I fixed this problem by disable the size classes, then enable it.
You can disable and enable the size classes in Interface builder doc.
It's very simple. You just need to use navigaitonItem first as the holder of the buttons and then you can add barButtonItems on top of that.

Sharing a UINavigationItem among different ViewControllers

I have a requirement to implement an app that has a navigation bar like bar at the top of numerous screens.
It has an icon on the left, some text, and some buttons on the right thus these would map well to a navigation item's left bar button items, title view, and right bar button icons.
However on most of the screens the content of the bar remains the same - i.e. a back button and title change would only appear occasionally for some screens, and on others the navigation bar would be present but is not actually used for navigational purposes.
Is the best of implementing this to configure a UINavigationItem, if so as there are multiple screens and multiple view controllers is there anyway the same UINavigationItem can be shared? That way I can configure the UINavigationItem in the RVC and keep it there as different view controllers get pushed, replacing it where need be when a back button does actually need to appear?
If this isn't the best approach then what alternatives are there? I experimented with making my RVC a container view controller and adding the bar as a view of that, that works for the immediate child view controllers but not for grandchild view controllers (which would take up the entire screen and not the portion alloted to them by the container view).
The navigationItem in UIViewController is readonly, so you can't have a single shared UINavigationItem shared between view controllers. You could have a base view controller class that manages setting the navigation item that you derive all of your view controllers from. To keep your classes from getting too coupled you could have it update the contents of the navigationItem from a NSNotification. Then you can just post a notification when you need all of the navigation items to be updated.

what UINavigationController do with object which calls pushViewController: function?

Is it removes object from superview or just hides that object?
Check the docs :) Its adding it to the stack, not removing it. popViewControllerAnimated: removes it.
The object in the viewController parameter becomes the top view
controller on the navigation stack. Pushing a view controller results
in the display of the view it manages. How that view is displayed is
determined by the animated parameter. If the animated parameter is
YES, the view is animated into position; otherwise, the view is simply
displayed in place. The view is automatically resized to fit between
the navigation bar and toolbar (if present) before it is displayed.
In addition to displaying the view associated with the new view
controller at the top of the stack, this method also updates the
navigation bar and tool bar accordingly. In iOS 3.0 and later, the
contents of the built-in navigation toolbar are updated to reflect the
toolbar items of the new view controller. For information on how the
navigation bar is updated, see “Updating the Navigation Bar.”
http://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:

Resources