Refresh UIToolbar - ios

I have created a subclass of UIToolbar. And setting toolbar items from that class. On ViewController, i took a toolbar and assign it a class subclass of UIToolbar. Its showing toolbar items on it.
The problem is that, when i update toolbar items its not showing on viewController. When i go to another view and come to this view its showing updated toolbar items.
I need an immediate refresh of toolbar items on that viewController.

In the viewController that contains your UIToolbar, call "[self.view setNeedsLayout]" (or even just [self.toolbar setNeedsLayout]) to force re-drawing of the graphics context.
On a different note, though: You should not need to subclass UIToolbar to just set the toolbar items. Create a UIToolbar and set its items array by calling [self.myToolbar setItems:#[item1, item2, ...]]

Related

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.

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

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

Does the iOS tabbar/toolbar support buttons/labels/images?

I am looking to have a UITableViewController that has a TableView with 2 sections and a tabbar/toolbar that holds two buttons, 1 share button and the other is a button that pushes a view controller. The table view should be underneath the tabbar/toolbar (or the frame should not go under the toolbar/tabbar). If toolbar/tabbar is not possible having a image/button I was thinking of making a view to have as a subview.
Here is what it should look like:
If anyone has any suggestions or ideas of how to go about this, any guidance would be appreciated. Thanks in advance!
First of all you want a UIToolbar not a UITabBar.
Secondly the only kind of element UIToolbar can hold is UIBarItem, specifically you are interested in its concrete subclass UIBarButtonItem.
UIBarButtonItem can either be a system item or hold a custom UIView. Since UIButton and UILabel are all subclasses of UIView you can place them inside a UIBarButtonItem and add this item to the toolbar.
Yes,you drag a tabbar to your viewcontroller, an then drag buttons or other things inside of that tabbar.

Changing toolbar item from toolbar subclass does not update toolbar immediately

I have a subclass of UIToolbar. I created it because I have the same toolbar on multiple pages(UIViewControllers). I took UIToolbar on my UIViewControllers (I am using storyboards) and assign this subclass to the UIToolbar class.
Also each item on that toolbar is created with a custom view (UIButton). When I change one of the items from its subclass, it's not reflecting immediately on the toolbar. I have to go to another screen and come back to view the updated result.
Is there any way to update immediately? Thanks.

Resources