【问题标题】:Adding bar button item in navigation bar在导航栏中添加栏按钮项
【发布时间】:2013-09-30 19:13:33
【问题描述】:

我正在使用导航栏按钮项。我正在使用以下代码来这样做

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

     UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
                                    initWithTitle:@"Cancel"                                            
                                    style:UIBarButtonItemStyleBordered 
                                    target:self 
                                    action:@selector(save_Clicked)];
     self.navigationItem.leftBarButtonItem = btnCancel;
     [btnCancel release];

我的问题是如何在左侧栏按钮项旁边添加另一个按钮。 提前谢谢

【问题讨论】:

    标签: iphone objective-c xcode


    【解决方案1】:

    为此,您需要创建一个工具栏,然后继续向其添加 UIButton,然后将工具栏设置为 leftBarButton

    类似这样的:

    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
    tools.tintColor = [UIColor clearColor];
    [tools setTranslucent:YES];
    
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:9];
    
    UIImage *myImage = [UIImage imageNamed:@"AL_HomeMod_Icon.png"];
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myButton setImage:myImage forState:UIControlStateNormal];
    myButton.showsTouchWhenHighlighted = YES;
    myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
    
    [myButton addTarget:self action:@selector(clickViewHomeMod) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *bi = [[UIBarButtonItem alloc]
                           initWithCustomView:myButton];
    
    [buttons addObject:bi];
    [bi release];
    
    myImage = [UIImage imageNamed:@"AL_History_Icon.png"];
    myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myButton setImage:myImage forState:UIControlStateNormal];
    myButton.showsTouchWhenHighlighted = YES;
    myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
    
    [myButton addTarget:self action:@selector(clickViewHistory) forControlEvents:UIControlEventTouchUpInside];
    
    bi = [[UIBarButtonItem alloc]
          initWithCustomView:myButton];
    
    [buttons addObject:bi];
    [bi release];
    
    myImage = [UIImage imageNamed:@"AL_RX_Icon.png"];
    myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myButton setImage:myImage forState:UIControlStateNormal];
    myButton.showsTouchWhenHighlighted = YES;
    myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
    
    [myButton addTarget:self action:@selector(clickViewCustomPopView2) forControlEvents:UIControlEventTouchUpInside];
    
    bi = [[UIBarButtonItem alloc]
          initWithCustomView:myButton];
    
    [buttons addObject:bi];
    [bi release];
    
    myImage = [UIImage imageNamed:@"AL_User_Icon.png"];
    myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myButton setImage:myImage forState:UIControlStateNormal];
    myButton.showsTouchWhenHighlighted = YES;
    myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
    
    [myButton addTarget:self action:@selector(clickViewCustomPopView:) forControlEvents:UIControlEventTouchUpInside];
    bi = [[UIBarButtonItem alloc]
          initWithCustomView:myButton];
    [buttons addObject:bi];
    popButton = myButton;
    [bi release];
    
    
    // stick the buttons in the toolbar
    [tools setItems:buttons animated:NO];
    
    [buttons release];
    
    // and put the toolbar in the nav bar
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
    [tools release];
    

    希望有所帮助

    池塘

    【讨论】:

      【解决方案2】:

      创建一个按钮为

      UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] 
                                      initWithImage:[UIImage imageNamed:@"logout.png"] 
                                      style:UIBarButtonItemStylePlain 
                                      target:self action:@selector(doLogout)];
      

      将此按钮添加到导航栏右侧

      self.navigationItem.rightBarButtonItem = logoutButton;
      

      或将此按钮添加到导航栏左侧

      self.navigationItem.leftBarButtonItem = logoutButton;
      

      doLogout 是一个将在触摸注销按钮时调用的函数

      【讨论】:

        【解决方案3】:

        我使用以下代码完成了我的任务:

        UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 44)];
        
        tools.backgroundColor=[UIColor clearColor];
        
         [tools setTranslucent:YES];
        
        UIBarButtonItem *optionBtn=[[UIBarButtonItem alloc]initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:self action:@selector(optionPressed:)];
        
        UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(donePressed:)];
        
         NSArray *buttons=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];
        
            [tools setItems:buttons animated:NO];
        
         self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:tools];
        

        注意:从 IOS 5.0 开始,Apple 做得更容易了。可以这样做

        self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];
        

        【讨论】:

          【解决方案4】:

          使用两个按钮创建一个自定义视图并使用UIBarButtonItem 的initWithCustomView: 初始化程序。应该这样做。

          【讨论】:

            猜你喜欢
            • 2012-08-21
            • 1970-01-01
            • 2018-07-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多