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

前端 未结 3 989
遥遥无期
遥遥无期 2021-01-02 20:15

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

相关标签:
3条回答
  • 2021-01-02 20:33

    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!

    0 讨论(0)
  • 2021-01-02 20:40
    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];
    
    0 讨论(0)
  • 2021-01-02 20:52

    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];
    
    0 讨论(0)
提交回复
热议问题