如何在UIToolbar中沿长度分配按钮?

时间:2013-04-15 18:32:09

标签: ios objective-c uibarbuttonitem uitoolbar

在我的应用程序中,我使用了一些代码,动态地将带有图像的按钮添加到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]];

但它运作不佳:

screenshot

如果我尝试设置另一种按钮样式:

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

看起来也很差:

screenshot 2

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:10)

添加两个使用系统样式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]];

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

答案 1 :(得分:3)

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

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

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

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

答案 2 :(得分:0)

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

//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;
}