【问题标题】:Multiple UIBarButtonItems in UINavigationBarUINavigationBar 中的多个 UIBarButtonItems
【发布时间】:2011-02-24 05:45:37
【问题描述】:

如何在导航栏中创建多栏按钮?

【问题讨论】:

    标签: iphone objective-c ipad uinavigationbar uibarbuttonitem


    【解决方案1】:

    从 iOS 5 开始,您现在可以使用 setLeftBarButtonItems:animated:setRightBarButtonItems:animated: 进行操作

    【讨论】:

    • 这现在比当前选择的答案更相关。
    【解决方案2】:

    您必须使用 UIToolbar 并设置带有按钮的工具栏:

    // create a toolbar where we can place some buttons
    UIToolbar *toolbar = [[UIToolbar alloc]
                            initWithFrame:CGRectMake(0, 0, 100, 45)];
    [toolbar setBarStyle: UIBarStyleBlackOpaque];
    
    // create an array for the buttons
    NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
    
    // create a standard save button
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemSave
        target:self
        action:@selector(saveAction:)];
    saveButton.style = UIBarButtonItemStyleBordered;
    [buttons addObject:saveButton];
    
    // create a spacer between the buttons
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
        target:nil
        action:nil];
    [buttons addObject:spacer];
    
    // create a standard delete button with the trash icon
    UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
        target:self
        action:@selector(deleteAction:)];
    deleteButton.style = UIBarButtonItemStyleBordered;
    [buttons addObject:deleteButton];
    
    // put the buttons in the toolbar and release them
    [toolbar setItems:buttons animated:NO];
    
    // place the toolbar into the navigation bar
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                               initWithCustomView:toolbar];
    

    【讨论】:

    • 这很好,但由于工具栏太高,旋转到横向时就不是很好了。不幸的是,我现在无法为此提供解决方案。
    • 干得好,因为该网站当前已关闭,所以它被逐字复制!
    • 像魅力一样工作。别管它最初是从哪里来的。将它放在 stackoverflow 上会更有用。
    【解决方案3】:
        you have to create a view with as much button you required and have to add them on navigation button like following :
    
        UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
            UIButton *infoButton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 30, 32)];
            [infoButton1 setBackgroundImage:[UIImage imageNamed: @"navbtn.png"] forState:UIControlStateNormal];
            [infoButton1 setTitle:@"Back" forState:UIControlStateNormal];
            infoButton1.titleLabel.font = [UIFont systemFontOfSize:13.0f];
            infoButton1.titleLabel.textColor = [UIColor whiteColor];
            [infoButton1 addTarget:self action:@selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
            [parentView1 addSubview:infoButton1];
            [infoButton1 release];
    
    UIButton *infoButton2 = [[UIButton alloc] initWithFrame:CGRectMake(30, 6, 30, 32)];
            [infoButton2 setBackgroundImage:[UIImage imageNamed: @"navbtn.png"] forState:UIControlStateNormal];
            [infoButton2 setTitle:@"Back" forState:UIControlStateNormal];
            infoButton2.titleLabel.font = [UIFont systemFontOfSize:13.0f];
            infoButton2.titleLabel.textColor = [UIColor whiteColor];
            [infoButton2 addTarget:self action:@selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
            [parentView1 addSubview:infoButton2];
            [infoButton2 release];
            UIBarButtonItem *customBarButtomItem1 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
            [parentView1 release];
            self.navigationItem.leftBarButtonItem = customBarButtomItem1;
            [customBarButtomItem1 release];`enter code here`
    

    【讨论】:

      【解决方案4】:

      我知道这个问题已经结束,但我发现 UIToolbar 解决方案在视觉上不匹配。

      如果您改为使用第二个 UINavigationBar 集和 UINavigationItem 的标题为 nil 和所需的按钮,您可以添加更多按钮并有一个在视觉上与原始匹配的栏。

      【讨论】:

      • 如果需要支持以下 iOS5 的好答案,但需要注意...如果您在视图控制器主 navigationItem 中设置了提示,这将不起作用(因为 UINavigationBar 在这种情况下为 74px 高)
      【解决方案5】:

      对于 iOS7 及更高版本,这是正确的做法。不需要 UIToolbar 愚蠢。

      - (void)viewDidLoad {
      
          [super viewDidLoad];
          [self configureView];
      
          // create three funky nav bar buttons
          UIBarButtonItem *one = [[UIBarButtonItem alloc]initWithTitle:@"One" style:UIBarButtonItemStylePlain target:self action:@selector(testMethod)];
          UIBarButtonItem *two = [[UIBarButtonItem alloc]initWithTitle:@"Two" style:UIBarButtonItemStylePlain target:self action:@selector(testMethod)];
          UIBarButtonItem *three = [[UIBarButtonItem alloc]initWithTitle:@"Three" style:UIBarButtonItemStylePlain target:self action:@selector(testMethod)];
      
          // create a spacer
          UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
          space.width = 30;
      
          NSArray *buttons = @[one, space, two, space, three];
      
          self.navigationItem.rightBarButtonItems = buttons;
      }
      

      我讨厌将链接作为答案放在 SO 上,因为它们随时可能死掉,所以我添加了取自 HERE 的相关代码

      【讨论】:

        【解决方案6】:
        - (void)viewWillAppear
        {
        
            // get a view and :
            [self.navigationController.navigationBar addSubView:yourView];
        
        }
        

        【讨论】:

        • 您是否会考虑添加一些叙述来解释此代码为何有效,以及是什么使它成为问题的答案?这对提出问题的人以及其他任何出现的人都非常有帮助。
        猜你喜欢
        • 2018-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-27
        • 1970-01-01
        相关资源
        最近更新 更多