【问题标题】:More than 1 rightBarButtonItem on navigation bar导航栏上超过 1 个 rightBarButtonItem
【发布时间】:2010-10-30 05:00:39
【问题描述】:

我想在导航栏上有两个 rightBarButtonItems。一个用于编辑,另一个用于添加。

显然我无法使用 Interface Builder。

有人知道如何以编程方式进行吗?谢谢!

【问题讨论】:

    标签: iphone navigation


    【解决方案1】:

    现在包含在 iOS 5 中,称为 rightBarButtonItems,注意复数

    这是来自apple docs的文字:

    rightBarButtonItems

    要在导航栏右侧显示的自定义栏按钮项数组 当接收者是顶部导航项时。

    @property(nonatomic, copy) NSArray *rightBarButtonItems

    讨论

    此数组可以包含 0 个或多个条形按钮项以显示在右侧
    导航栏。项目从右到左显示的顺序与它们在 大批。因此,数组中的第一项是最右边的项,其他项被添加 上一项的左侧。

    如果没有足够的空间来显示数组中的所有项目,那些会 重叠标题视图(如果存在)或栏左侧的按钮不
    显示出来。

    数组中的第一项也可以使用 rightBarButtonItem 属性来设置。

    在 UINavigationBar.h 中声明

    这是我在导航栏右侧实现搜索图标和编辑图标的方法:

    UIBarButtonItem *searchButton         = [[UIBarButtonItem alloc]
                                             initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                             target:self
                                             action:@selector(searchItem:)];
    
    UIBarButtonItem *editButton          = [[UIBarButtonItem alloc] 
                                             initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
                                             target:self action:@selector(editItem:)];
    
    self.navigationItem.rightBarButtonItems =
    [NSArray arrayWithObjects:editButton, searchButton, nil];
    

    【讨论】:

    • 在发行说明中完全错过了这一点。谢谢!
    • 是的,我也这样做了,直到 SO 的某个人向我提示!
    • +1 感谢您花时间更新这篇旧帖子。帮了大忙。
    【解决方案2】:

    这是一个如何添加两个按钮作为右栏按钮的示例。下面的代码创建了一个包含向上和向下箭头按钮的分段控件,然后将其添加为导航右栏按钮的自定义视图:

    UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
        [segmentedControl setMomentary:YES];
        [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-up.png"] atIndex:0 animated:NO];
        [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-down.png"] atIndex:1 animated:NO];
        segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
    
        UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
        self.navigationItem.rightBarButtonItem = segmentBarItem;
    

    【讨论】:

    • 这对我帮助很大。谢谢! :)
    • 这确实有很大帮助。只是不知道为什么它没有被接受。
    • 当我想在我自己的应用程序的右侧放置两个按钮时,这并不是我的想法,但它的编码和运行比人们破解的各种方式要干净得多一起完成它,我将把它接管我脑海中的原始图像。
    【解决方案3】:

    我的建议是不要将添加功能实现为导航栏中的按钮。我假设您正在处理下面项目的表格视图,因此处理此用户交互的一种方法是将“添加新项目”选项显示为表格视图中的最后一个条目。当用户通过实现以下委托方法点击导航栏中的“编辑”按钮时,这可以通过编程方式淡入:

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];
        [self.tableView beginUpdates];
        [self.tableView setEditing:editing animated:YES];
    
        if (editing)
        {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];           
        }
        else
        {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0];     
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];           
        }
        [self.tableView endUpdates];
    }
    

    然后,您需要通过使用以下方法增加行数来确保额外的行数:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {
        if (tableView.editing)
            return ([objects count] + 1);
        else
            return [objects count];     
    }
    

    然后在其左侧显示绿色加号,与正常的删除编辑样式相反:

    - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
        if (indexPath.row >= [objects count]) 
            return UITableViewCellEditingStyleInsert;
        else
            return UITableViewCellEditingStyleDelete;
    
        return UITableViewCellEditingStyleNone;
    }
    

    当然,您需要在 cellForRowAtIndexPath: 实现中为其提供一个名称并处理其行选择。

    【讨论】:

      【解决方案4】:

      如果有人路过,这里是快速的答案:

      let barButton_array: [UIBarButtonItem] = [Button1, Button2]
      navigationItem.setRightBarButtonItems(barButton_array, animated: false)
      

      【讨论】:

        【解决方案5】:

        导航栏是一个 UIView,因此您可以简单地创建一个规则 UIButton 并将其作为子视图添加到您的导航栏。

        设置相对于导航栏的框架。如果您希望它看起来与内置按钮完全一样,您可能必须自己生成图形,因为它们不会暴露给 SDK AFAIK。

        【讨论】:

        • 这真的有效吗?我试过没有 luc _viewTypeButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,20,20)]; _viewTypeButton.titleLabel.text=@"R"; [self.navigationController.navigationBar addSubview:_viewTypeButton]; k.
        【解决方案6】:

        如果您希望有两个单独的按钮作为rightBarButtonItem,您可以通过将UIToolbar 作为自定义视图添加到右侧栏项来实现:

        /*************************************************
               CREAT TWO RIGHT BAR BUTTON ITEMS
           *************************************************/
            // create a toolbar to have two buttons in the right
            UIToolbar* customToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)];
        
            // create the array to hold the buttons, which then gets added to the toolbar
            NSMutableArray* rightBarButtonArray = [[NSMutableArray alloc] initWithCapacity:2];
        
            //Add the info button to the array
            UIButton* infoViewButton = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
            [infoViewButton addTarget:self action:@selector(showInfoView) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem *infoItem =  [[UIBarButtonItem alloc] initWithCustomView:infoViewButton];
            [rightBarButtonArray addObject:infoItem];
            [infoItem release];
        
            //Add the Done Button to the array
            UIBarButtonItem *bbi;
            bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                                target:self 
                                                                action:@selector(create:)];
        
            [rightBarButtonArray addObject:bbi];
            [bbi release];
        
            //add the array to the custom toolbar
            [customToolbar setItems:rightBarButtonArray animated:NO];
            [rightBarButtonArray release];
        
            // and finally add the custom toolbar as custom view to the right bar item
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customToolbar];
            [customToolbar release];
        

        【讨论】:

          【解决方案7】:

          这是非常简单的两行答案:

          第 1 步:为自定义视图创建一个笔尖,包含您想要的任何内容

          第 2 步:将 nib 作为自定义视图添加到工具栏:

          NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"TwoButtonView" owner:self options:nil];
          self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[subviewArray objectAtIndex:0]];
          

          【讨论】:

            【解决方案8】:

            显示独立按钮(而不是分段控制按钮):

            // create a toolbar to have two buttons in the right
            UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
            
            // create the array to hold the buttons, which then gets added to the toolbar
            NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
            
            // create a standard "add" button
            UIBarButtonItem* bi = [[UIBarButtonItem alloc]
            initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
            bi.style = UIBarButtonItemStyleBordered;
            [buttons addObject:bi];
            
            // create a spacer
            bi = [[UIBarButtonItem alloc]
            initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            [buttons addObject:bi];
            
            // create a standard "refresh" button
            bi = [[UIBarButtonItem alloc]
            initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
            bi.style = UIBarButtonItemStyleBordered;
            [buttons addObject:bi];
            
            // stick the buttons in the toolbar
            [tools setItems:buttons animated:NO];
            
            // and put the toolbar in the nav bar
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
            

            【讨论】:

              【解决方案9】:

              您可以从 Interface Builder 中进行操作。我在我的应用程序中所做的 - 只是在导航栏的左侧项目上添加了 UIView 并在此视图中放置了 3 个按钮并连接了它们的操作。您可以按照此在左/右导航项中添加多个按钮。

              参考:Toolbar items in sub-nib

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-09-15
                相关资源
                最近更新 更多