【问题标题】:How to distribute buttons along length in UIToolbar?如何在 UIToolbar 中沿长度分布按钮?
【发布时间】:2013-04-07 23:43:19
【问题描述】:

在我的应用程序中,我使用一些代码将带有图像的按钮动态添加到UIToolbar

[self.navigationController setToolbarHidden:NO];
UIImage *buttonImage1 = [UIImage imageNamed:@"1.png"];
UIImage *buttonImage2 = [UIImage imageNamed:@"2.png"];
UIImage *buttonImage3 = [UIImage imageNamed:@"3.png"];

UIBarButtonItem *toolButton1 = [[UIBarButtonItem alloc] initWithImage:buttonImage1 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];
UIBarButtonItem *toolButton2= [[UIBarButtonItem alloc] initWithImage:buttonImage2 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];
UIBarButtonItem *toolButton3 = [[UIBarButtonItem alloc] initWithImage:buttonImage3 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];

[self setToolbarItems:[NSArray arrayWithObjects:toolButton1, toolButton2, toolButton3, nil]];

但效果不好:

如果我尝试设置其他按钮样式:

toolButton1.style = UIBarButtonSystemItemFlexibleSpace;
toolButton2.style = UIBarButtonSystemItemFlexibleSpace;
toolButton3.style = UIBarButtonSystemItemFlexibleSpace;

它看起来也很糟糕:

我该如何解决这个问题?

【问题讨论】:

标签: ios objective-c uibarbuttonitem uitoolbar


【解决方案1】:

添加两个使用系统样式UIBarButtonSystemItemFlexibleSpace 的附加栏按钮,并在每个现有按钮之间放置一个:

[self.navigationController setToolbarHidden:NO];
UIImage *buttonImage1 = [UIImage imageNamed:@"1"];
UIImage *buttonImage2 = [UIImage imageNamed:@"2"];
UIImage *buttonImage3 = [UIImage imageNamed:@"3"];

UIBarButtonItem *toolButton1 = [[UIBarButtonItem alloc] initWithImage:buttonImage1 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];
UIBarButtonItem *toolButton2= [[UIBarButtonItem alloc] initWithImage:buttonImage2 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];
UIBarButtonItem *toolButton3 = [[UIBarButtonItem alloc] initWithImage:buttonImage3 style:UIBarButtonItemStylePlain target:self action:@selector(btnSettingsClick:)];

[self setToolbarItems:[NSArray arrayWithObjects:
    toolButton1, 
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
    toolButton2, 
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
    toolButton3, 
    nil]];

概念化很奇怪,但灵活空间实际上是一个独特的对象,而不是适用于其他对象的样式。

【讨论】:

  • 很好的答案,但旁注不正确。无论是否指定 .png 扩展名,@2x 后缀都可以正常工作。事实上,如果不是.png,则需要扩展名(可以使用@2x jpeg图片)。
【解决方案2】:

您需要使用灵活的空间来让按钮沿着工具栏的长度分布。 在按钮之前,每个按钮之间,在按钮之后,您应该有一个灵活的空格按钮。 (UIBarButtonSystemItemFlexibleSpace)

UIBarButtonItem *flexibleSpaceBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]

工具栏项数组应该是这样的:

[self setToolbarItems:[NSArray arrayWithObjects:toolButton1, flexibleSpaceBtn1, toolButton2, flexibleSpaceBtn2, toolButton3, nil]];

【讨论】:

    【解决方案3】:

    这是我使用的一些参考代码 -

    //Constants
    let imageNames : [String] = ["img1.png", ... "imgN.png"];
    
    /********************************************************************************/
    /** @fcn        spacingDemo()
     *  @brief      space all toolbar items evenly across the UIToolbar on keyboard
     *
     *  @param      [in] (UITextView) textView : view to attach keyboard to in response
     */
    /********************************************************************************/
    func spacingDemo() {
    
        //Vars
        var barButtons : [UIBarButtonItem];
        var button : UIButton;
        var img    : UIImage;
    
        //Init
        keyboardToolbar = UIToolbar();
        barButtons      = [UIBarButtonItem]();
    
        //Config
        keyboardToolbar.barTintColor = UIColor.lightGray;   /* set bkgnd color      */
        let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil);
        barButtons.append(flexBarButton);                   /* size-to-fit          */
    
        for imageName in imageNames {
    
            //Gen Button
            button = UIButton(type: .custom);
            button.setImage(UIImage(named: imageName), for: .normal);
            button.addTarget(self, action:  #selector(self.keyboardResponse), for: .touchUpInside);
            barButtons.append(UIBarButtonItem(customView: button));
    
            //Apply Spacing
            let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil);
            barButtons.append(flexBarButton);               /* size-to-fit          */
        }
    
        //Assemble
        keyboardToolbar.items = barButtons;
    
        //Attach
        textView.inputAccessoryView = keyboardToolbar;
    
        //Cleanup
        keyboardToolbar.sizeToFit();
    
        return;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多