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

iosipaduinavigationbar

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

Best Answer

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!

Related Topic