8

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

它看起来也很糟糕:

截图 2

我怎样才能解决这个问题?

4

3 回答 3

11

添加两个使用系统样式的附加栏按钮,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]];

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

于 2013-04-15T18:41:02.540 回答
3

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

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

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

[self setToolbarItems:[NSArray arrayWithObjects:toolButton1, flexibleSpaceBtn1, toolButton2, flexibleSpaceBtn2, toolButton3, nil]];
于 2013-04-15T18:41:22.720 回答
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;
}
于 2018-02-18T16:27:53.513 回答