I have a navigationController which has a navigation bar. I would really like to have 3 UIBarButtonItems, one on the left, one in the middle, and one on the right. I am able to get the left and right ones added, but how would I add one in the middle, since when using a navigationController, I can't add an array of items to the navBar items property?
Could I somehow add a UIButton, styled like a UIBarButtonItem in the titleView location?
taken from apple api, basically you can create a custom UIView that has a UIButton in it and use this as your titleView (notice the note about a leftBarButtonItem causes the titleView to be ignored and not shown): (edit note: this is a property of UINavigationItem)
titleView
A custom view displayed in the center of the navigation bar when this item is the top item.
#property(nonatomic, retain) UIView *titleView
Discussion
If this property value is nil, the navigation item’s title is displayed in the center of the navigation bar when this item is the top item. If you set this property to a custom title, it is displayed instead of the title. This property is ignored if leftBarButtonItem is not nil.
Custom views can contain buttons. Use the buttonWithType: method in UIButton class to add buttons to your custom view in the style of the navigation bar. Custom title views are centered on the navigation bar and may be resized to fit.
Related
I have a view controller that's embedded in a navigation controller. Therefore this view controller has a navigation item at the top. One of the things you can do in ios6 storyboards is that you can set the title, prompt and back button for this view controller (because its embedded in a navigation controller).
That being said, when I specify a title and test the app, everything looks good. However, when I place a transparent UIView on top of the navigation item (such as where the title is), the title itself just vanishes. The text itself that I typed into interface builder is gone. I have proven this because if I delete the view I created, the text I initially had is gone.
I tried to mediate the problem by actually setting the title itself outside of the interface builder:
self.navigationItem.title=#"My Title";
But that doens't seem to work either. Does anyone have a clue as to how I can hide/show a transparent UIView on top of a navigationItem in a navigation controller?
EDIT
Any UI element I place in the navigation controller toolbar seems to prevent the underlying title text from showing up. This happens even if the element is marked as transparent AND its set to hidden.
My understanding is that Interface Builder is, more or less, mimicking what you would do if you did the same thing programmatically using the UIBarButtonItem class. The various items in a navigation bar are instances of the UIBarButtonItem class. This class has the following initializers:
– initWithBarButtonSystemItem:target:action:
– initWithCustomView:
– initWithImage:style:target:action:
– initWithTitle:style:target:action:
– initWithImage:landscapeImagePhone:style:target:action:
When you just have a title for the navigation controller, Interface Builder treats it similar to using the initWithTitle: initializer. Basically, this means that, under the hood, a UILabel class is created with the given title and that UILabel is used as the view for the UIBarButtonItem.
When you are dragging the transparent view over the title, however, Interface Builder is instead doing the equivalent of calling initWithCustomView:. This means that the view you are providing is being used as the UIBarButtonItem's view. In other words, when you drag the custom view over the title, you are not placing it on top of the title. You are replacing the title with the transparent view.
One option might be to create a view which has both a UILabel and the transparent view as subviews. Then place that view as the title for the navigation bar. If you give that UILabel the correct font size and shadow, it will look indistinguishable from the system's default title and you will also be able to have the transparent view on top of it.
In IB, you can drag a UIView to the center of the navigation bar, and this will replace the titleView that is there by default (you can do it in code with setTitleView:). If you make its background clear and add a label to it to hold the title, it will look like the default title. You can then add another UIView as the subview of this view, just like you would with any other UIView.
I'm trying to change a UINavigationController navigation bar to my custom navigation bar (subclass of UINavigationBar) with IB.
So I have a xib file with a UINavigationController, but when I stand on the navigation bar and go to the inspector to change it's class, It shows me a UINavigationItem class not UINavigationBar, and I can't choose my custom navigation bar.
What am I doing wrong?
To answer my own question, I found the solution in this post
Only after I tapped on the "Show Document Outline" button (the rounded arrow at the left bottom), I could find the navigationBar under the navigation controller, and change his class.
I am trying to add a UIButton to a UINavigationBar with the following:
UIButton * addSource = [UIButton buttonWithType:UIButtonTypeCustom];
[addSource setBackgroundImage:[UIImage imageNamed:#"addsource.png"] forState:UIControlStateNormal];
[addSource addTarget: self action:#selector(addSourceButton:) forControlEvents:UIControlEventTouchUpInside];
[addSource setFrame: CGRectMake(115, 5, 32, 32)];
[navBar addSubview:addSource];
[addSource release];
However this doesn't work, any idea? When I comment out the setBackgroundImage and change it to setBackgroundColor I can see it, but I can't seem to click on it (i.e: the action in which it's set to is not triggered). Any idea? Changing it to a UIImageView instead of a UIButton also works, I can see the image just fine, so this clarifies that the image is there.
You can not add a UIButton to a UINavigationBar. Use a UIBarButtonItem.
From Apple docs:
Unlike other types of views,
you do not add subviews to a navigation bar directly. Instead, you use
a navigation item (an instance of the UINavigationItem class) to
specify what buttons or custom views you want displayed.
and:
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.
”
Adding Content to a Navigation Bar
When you use a navigation bar as a standalone object, you are responsible for providing its contents. Unlike other types of views, you do not add subviews to a navigation bar directly. Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed. A navigation item has properties for specifying views on the left, right, and center of the navigation bar and for specifying a custom prompt string.
A navigation bar manages a stack of UINavigationItem objects. Although the stack is there mostly to support navigation controllers, you can use it as well to implement your own custom navigation interface. The topmost item in the stack represents the navigation item whose contents are currently displayed by the navigation bar. You push new navigation items onto the stack using the pushNavigationItem:animated: method and pop items off the stack using the popNavigationItemAnimated: method. Both of these changes can be animated for the benefit of the user.
In addition to pushing and popping items, you can also set the contents you could also use of the stack directly using either the items property or the setItems:animated: method. You might use these methods at launch time to restore your interface to its previous state or to push or pop more than one navigation item at a time.
If you are using a navigation bar as a standalone object, you should assign a custom delegate object to the delegate property and use that object to intercept messages coming from the navigation bar. Delegate objects must conform to the UINavigationBarDelegate protocol. The delegate notifications let you track when navigation items are pushed or popped from the stack. You would use these notifications to update the rest of your application’s user interface.
For more information about creating navigation items, see UINavigationItem Class Reference. For more information about implementing a delegate object, see UINavigationBarDelegate Protocol Reference."
from UiNavigationBar class reference.
please also refer UIBarButoonItem -(id)initWithCustomView:(UIView*)view. pleae note UIbutton is subclass of uiview
also refer uinavbaritem's
rightBarButtonItem property
– setLeftBarButtonItems:animated:
– setLeftBarButtonItem:animated:
– setRightBarButtonItems:animated:
– setRightBarButtonItem:animated:
and titleview.
you have to use a UIBarButton item, heres the code:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(dismissView)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
If you want to add something in the centre in your navigation bar, why don't you just do
navbar.titleView = addSource
I have a UIViewController. I want to add a UIToolbar to the bottom, but IB is not letting me. How can I achieve this?
Note:
The UIViewController is part of a UINavigationController. I'm not sure if this affects the visibility of the toolbar.
Starting from iOS 3.0 you can set toolbar items you want using following UIViewController's method:
- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
of simply set or override toolbarItems property.
And toolbar will appear automatically if you put your view controller inside navigation controller.
You can't add toolbar in IB, in IB all you can do is - set a dummy toolbar / navigation bar / tabBar etc. so that you can simulate them to arrange your view's subviews appropriately.
To set the toolbar for that viewController, you have to fill the UIViewController's:
setToolbarItems:animated:
or
#property(nonatomic, retain) NSArray *toolbarItems
so this toolbar and its items will be displayed when your viewController is presented.
I am looking to add a bar with buttons (buttons to fire actions, not for navigation) under the statusBar and above a custom UIView. My question is, I am not sure if I should be using UINavigationBar and adding UIBarButtonItem or if I should be using UIToolBar (which says it is used at the bottom of the screen) again adding UIBarButtonItem for the button?
You would usually use a UINavigationController for this and set the leftBarButtonItem and rightBarButtonItem of the navigationItem property of the view controller for the custom UIView.
Using UIToolbar is probably not what you want since it has a double border at the top, and a less visible border at the bottom, which would look weird in a toolbar at the top of the interface.