I am trying to put a label in a UIBarButtonItem. Here is my viewDidLoad method:
characterCountLabel = [[UILabel alloc] init];
characterCountLabel.text = #"HELLO";
charCountButton = [[UIBarButtonItem alloc]initWithCustomView:characterCountLabel];
Everything is hooked up properly in IB. Why is the label not appearing inside of my UIBarButtonItem?
A couple things come to mind. First, your label has no frame. Try using
characterCountLabel = [[UILabel alloc] initWithFrame:
CGRectMake(0.0f, 0.0, 200.0f, 25.0f)];
Next, can you show us the code where you actually add the bar button item? What are you adding it to--a navigation bar or a toolbar?
When you say everything is hooked up in IB, what are you referring to? The toolbar? Navigation bar? Are you using a navigation controller? If not, then you should be able to just add an item to a navigation bar in IB.
A little more information would help us help you.
Related
In my viewDidLoad method I have the following:
navigBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonSystemItemAction target:self action:#selector(btnClicked:)];
[self.view addSubview:navigBar];
The button does not show up at all! What am I missing?
// create the navigation bar and add it to the view
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navigationBar];
// create a button
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonSystemItemAction target:self action:#selector(btnClicked:)];
// create a UINavigationItem and add the button in the right hand side
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];
navItem.rightBarButtonItem = button;
// add the UINavigationItem to the navigation bar
[navigationBar pushNavigationItem:navItem animated:NO];
set button on navigation bar .then u create a BarButtonItem on navigation.
barbutton=[UIBarButtonItem alloc] ]initWithTitle......
navigation.navigationController.rightBarButton= barbutton;
First read this :
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.
So basically what you need to do is :
[navigBar pushNavigationItem:self.navigationItem animated:NO];
I've got a UIView with a NavigationBar and I'd like to add a button which looks like the style of a Back Button. I'm not using a UINavigationController and was wondering if this could be done without it?
The style of the button should look like this:
Thanks
You need to set up a custom stack of UINavigationItem objects and push them on to the UINavigationBar. This is the only way I know of to get a true back button. I haven't tested this code, but you should do something like this:
UINavigationItem *previousItem =
[[[UINavigationItem alloc] initWithTitle:#"Back title"] autorelease];
UINavigationItem *currentItem =
[[[UINavigationItem alloc] initWithTitle:#"Main Title"] autorelease];
[navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil]
animated:YES];
To handle when the buttons are pressed you should set yourself as the navigation bar's delegate and implement the UINavigationBarDelegate delegates.
You can also update this by modifying the backBarButtonItem on the previous view controller (not the currently viewed one).
In my application, I'm launching a modalViewController on rightbarbutton click of navigationbar. This modalViewController overlaps the navigationbar. I tried setting its frame but it remained the same. I want to display navigationbar even if modalViewController is still there.
EDIT: I call the following method on navigationcontroller's rightbarbutton press. The view appears properly but the position is not right.
-(void)showViewForPosts{
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"DISMISS"
style: UIBarButtonItemStyleBordered
target:self
action:#selector(dismissViewCOntroller)];
displayController = [[UIViewController alloc]initWithNibName:nil bundle:nil];
displayController.view.frame = CGRectMake(0.0, 150.0, 320, 436);
displayController.view.backgroundColor = [UIColor blueColor];
//I'M ADDING DIFFERENT VIEWS HERE
[self presentModalViewController:displayController animated:YES];
}
Thanx in advance.
Do you need it to be the same navigation bar?
In my app, there is another NavigationController for the modal view flow, ie going from a modal view to another. This feels more consistent to me, since 'modal' means taking (temporary) exclusive access to screen and inputs, whereas navigation on iPhone is stacking views.
Essentially i want to display a notification just beneath the UINavigationController, covering the area that would be occupied by the top most UIViewController's view. However i want the background image to extend upwards and partly cover the UINavigationBar
The finished product should hopefully look like the below
The trick comes in that i only want this to apply to certain views with the view Hierarchy, so attacking the problem by using [UIApplication keyWindow] might not be the best idea. Also i assume that by using views outside of the private UINavigationTransitionView won't be part of the view transition animation, leaving my view on top of the controller beneath when this one is popped.
The view of the topmost view controller doesn't help either, as it's frame is terminates at the base of UINavigationBar, meaning arrow won't overlap the UINavigationBar
Suggestions?
Thanks
Since it seems that you are facing the problem with Navigation Bar only as that will not allow to overlap the View.
This is tricky part....
Just use the Custom back button set the image over it in left of the Navigation bar Button
UINavigationBar *bar = [self.navigationController navigationBar];
UIButton *btn;
btn = [[UIButton alloc] initWithFrame:CGRectMake(5, 2, 100, 48)];
[btn setImage:[UIImage imageNamed:#"left.png"] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
[bar addSubview:btn];
[btn release];
This is surely going to help you & solve all your Queries for the same ....
I want to add a bar right below NavigationController which will visible all the time even as user scrolls down. Can you tell me how I can do that or refer me to an article? I want to add couple of buttons to it to sort out results.
Thanks
If the youtube app most viewed section (http://www.engadget.com/photos/the-definitive-iphone-user-interface-gallery/#294309) is what you are after, then you don't need a bar below the navigation bar.
You could make a UISegmentedControl and add it as a custom title view on the navigation item of your view controller. Then if you want a title to also be displayed above the buttons you would set the prompt property on the navigation item.
So, in the init method of view controller that has the scrollable view:
self.navigationItem.prompt = #"Title of this view";
NSArray *items = [NSArray arrayWithObjects:#"Sort1", #"Sort2", nil];
UISegmentedControl *control = [[[UISegmentedControl alloc] initWithItems:items]
autorelease];
[control addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = control;
As far as I know, it is impossible to nest multiple navigation controllers.