How to add a right button to a UINavigationController?

后端 未结 21 992
不思量自难忘°
不思量自难忘° 2020-11-28 18:25

I am trying to add a refresh button to the top bar of a navigation controller with no success.

Here is the header:

@interface PropertyViewController          


        
相关标签:
21条回答
  • 2020-11-28 18:44

    For swift 2 :

    self.title = "Your Title"
    
    var homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("yourMethod"))
    
    var logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("yourMethod"))
    
    self.navigationItem.leftBarButtonItem = homeButton
    self.navigationItem.rightBarButtonItem = logButton
    
    0 讨论(0)
  • 2020-11-28 18:44

    This issue can occur if we delete the view controller or try to add new view controller inside the interface builder(main.storyboard). To fix this issue, it requires to add "Navigation Item" inside new view controller. Sometimes it happens that we create new view controller screen and it does not connect to "Navigation Item" automatically.

    1. Go to the main.storyboard.
    2. Select that new view Controller.
    3. Go to the document outline.
    4. Check view Controller contents.
    5. If new view controller does not have a Navigation item then, copy Navigation item from previous View Controller and paste it into the new view controller.
    6. save and clean the project.
    0 讨论(0)
  • 2020-11-28 18:45
        self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshData)];
    
    
    
    }
    
    -(void)refreshData{
        progressHud= [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        [progressHud setLabelText:@"拼命加载中..."];
        [self loadNetwork];
    }
    
    0 讨论(0)
  • 2020-11-28 18:46

    You can try

    self.navigationBar.topItem.rightBarButtonItem = anotherButton;
    
    0 讨论(0)
  • 2020-11-28 18:49
    - (void)viewWillAppear:(BOOL)animated
    {    
        [self setDetailViewNavigationBar];    
    }
    -(void)setDetailViewNavigationBar
    {
        self.navigationController.navigationBar.tintColor = [UIColor purpleColor];
        [self setNavigationBarRightButton];
        [self setNavigationBarBackButton];    
    }
    -(void)setNavigationBarBackButton// using custom button 
    {
       UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"  Back " style:UIBarButtonItemStylePlain target:self action:@selector(onClickLeftButton:)];          
       self.navigationItem.leftBarButtonItem = leftButton;    
    }
    - (void)onClickLeftButton:(id)sender 
    {
       NSLog(@"onClickLeftButton");        
    }
    -(void)setNavigationBarRightButton
    {
    
      UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(onClickrighttButton:)];          
    self.navigationItem.rightBarButtonItem = anotherButton;   
    
    }
    - (void)onClickrighttButton:(id)sender 
    {
       NSLog(@"onClickrighttButton");  
    }
    
    0 讨论(0)
  • 2020-11-28 18:51
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 110, 50)];
    view.backgroundColor = [UIColor clearColor];
    
    UIButton *settingsButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    [settingsButton setImage:[UIImage imageNamed:@"settings_icon_png.png"] forState:UIControlStateNormal];
    [settingsButton addTarget:self action:@selector(logOutClicked) forControlEvents:UIControlEventTouchUpInside];
    [settingsButton setFrame:CGRectMake(40,5,32,32)];
    [view addSubview:settingsButton];
    
    UIButton *filterButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    [filterButton setImage:[UIImage imageNamed:@"filter.png"] forState:UIControlStateNormal];
    [filterButton addTarget:self action:@selector(openActionSheet) forControlEvents:UIControlEventTouchUpInside];
    [filterButton setFrame:CGRectMake(80,5,32,32)];
    [view addSubview:filterButton];
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    
    0 讨论(0)
提交回复
热议问题