1

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?

3 Answers 3

4
// 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];
Sign up to request clarification or add additional context in comments.

Comments

3

set button on navigation bar .then u create a BarButtonItem on navigation.

barbutton=[UIBarButtonItem alloc] ]initWithTitle......
navigation.navigationController.rightBarButton= barbutton;

1 Comment

I don't really understand what you are saying.
3

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.