Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Bar Button in navigation bar without navigation controller.

I am new to iOS development.I have create a navigation bar in my iPad application view.I don't need navigation controller that's why i have added only navigation bar. Now i want to add button in that navigation bar.I tried a lot but no success. Is it possible to add only navigation bar with button ? If yes then suggest me some sample code.

"i don't have navigation controller or need it. i just want to add navigation bar in only one view."

Bellow is my code which i write for adding navigation bar in ViewDidLoad()

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)];
[navBar setTintColor:[UIColor blackColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];

Thanks in advance....

like image 494
Bhavin_m Avatar asked Apr 11 '12 04:04

Bhavin_m


3 Answers

I used the Interface Builder to make a static tableView in a UITableViewController. This UITableViewController is shown modally. Then I added a NavigationBar without the UINavigationController behind it as follows:

//Creating the plain Navigation Bar
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

//The UINavigationItem is neede as a "box" that holds the Buttons or other elements
UINavigationItem *buttonCarrier = [[UINavigationItem alloc]initWithTitle:@"Sign-In"];

//Creating some buttons:
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Zurück" style:UIBarButtonItemStyleDone target:self action:@selector(signInBackPressed:)];
UIBarButtonItem *barDoneButton = [[UIBarButtonItem alloc] initWithTitle:@"Fertig" style:UIBarButtonItemStylePlain target:self action:@selector(signInDonePressed:)];

//Putting the Buttons on the Carrier
[buttonCarrier setLeftBarButtonItem:barBackButton];
[buttonCarrier setRightBarButtonItem:barDoneButton];

//The NavigationBar accepts those "Carrier" (UINavigationItem) inside an Array
NSArray *barItemArray = [[NSArray alloc]initWithObjects:buttonCarrier,nil];

// Attaching the Array to the NavigationBar
[headerView setItems:barItemArray];

// Adding the NavigationBar to the TableView 
[self.tableView setTableHeaderView:headerView];

I hope this helps somebody!

like image 66
Ekkstein Avatar answered Nov 16 '22 07:11

Ekkstein


UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editButton)];

bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f];

self.navigationItem.rightBarButtonItem = bi1;

[bi1 release];
like image 32
freelancer Avatar answered Nov 16 '22 06:11

freelancer


You can add buttons to navigation bar as follows:

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] 
                                initWithTitle:@"Save"
                                style:UIBarButtonItemStyleBordered 
                                target:self 
                             action:@selector(save_Clicked:)];
 navBar.rightBarButtonItem = btnSave;
 [btnSave release];

 UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
                                initWithTitle:@"Cancel"                                    
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(cancel_Clicked:)];
 navBar.leftBarButtonItem = btnCancel;
 [btnCancel release];
like image 1
Abdullah Md. Zubair Avatar answered Nov 16 '22 05:11

Abdullah Md. Zubair