使用背景图像自定义UIBarbuttonitem

3
我已经添加了一个UIToolbar实例和按钮。每个按钮都属于UIBarButtonItem类。
我的要求是,每个按钮都有自定义布局,我不想使用Apple提供的本地按钮样式。因此,在Interface Builder中我有3个选项(普通、有边框、完成)。我选择了普通样式,并在Bar item->Image下选择了我想要添加为背景的图像。
但这对我没有用,使用普通选项让我更接近边框和完成,但它仍然以白色轮廓显示图像。是否有任何方式可以添加按钮上的图像并使其看起来完全相同?这在使用UIButton时非常简单。我只需在UIButton的属性检查器中使用Image选项并选择所需的图像即可。但是,在使用UIBarButtonitem时,它根本不起作用。如下图所示:
[图片]
谢谢 Taimur
4个回答

24

Taimur,

我遇到了你遇到的同样问题。我们的设计师为我们应用程序的导航栏指定了自定义按钮外观。这里是我编写的一个实用方法,用于生成我们所需的UIBarButtonItems,您应该能够修改它以满足您的需求:

+ (UIBarButtonItem *)createSquareBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // Since the buttons can be any width we use a thin image with a stretchable center point
    UIImage *buttonImage = [[UIImage imageNamed:@"SquareButton.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    UIImage *buttonPressedImage = [[UIImage imageNamed:@"SquareButton_pressed.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];

    [[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
    [button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
    [[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];

    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0;
    buttonFrame.size.height = buttonImage.size.height;
    [button setFrame:buttonFrame];

    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

    [button setTitle:t forState:UIControlStateNormal];

    [button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return [buttonItem autorelease];
}

非常感谢您。 我刚试着使用您的代码,但在运行时崩溃了。 这是我的调用方式。 UIBarButtonItem *HomeButton = [self createSquareBarButtonItemWithTitle:@"主页" target:self action:@selector(pressHomeButton:)]; - Taimur Ajmal
我已经在同一个函数中编写了整个代码,并且现在它正在工作。这是一种糟糕的编程方式,因为我不得不将代码复制5次并粘贴到同一个函数中。但是当我调用该函数时,应用程序崩溃了。如果我在调用函数时出错,请指导我。 - Taimur Ajmal
它崩溃了,因为它是一个类方法(注意前面的+),而你正在实例上调用它。调用应该是 UIBarButtonItem *HomeButton = [[self class] createSquareBarButtonItemWithTitle:@"Home" target:self action:@selector(pressHomeButton:)];。或者,如果你从其他地方调用它,只需将类的名称放在[self class]的位置即可。 - Zev Eisenberg

3
问题在于UIBarButtonItem不是UIView子类。我也遇到了同样的问题,解决方案非常简单:创建一个带有自定义背景图像的UIButton对象,然后使用它的initWithCustomView:方法创建一个UIBarButtonItem实例,并将你的UIButton对象作为参数传递。希望我能帮到你。

谢谢,伙计。不幸的是,应用程序崩溃了。UIButton *BackButton = [UIButton alloc];[BackButton setImage:[UIImage imageNamed:@"Home_icon.png"] forState:UIControlStateNormal];UIBarButtonItem *HomeButton = [[UIBarButtonItem alloc] initWithCustomView:BackButton target:self action:@selector(pressHomeButton:)]; - Taimur Ajmal
嗯,那基本上也是我所做的。你有一些控制台输出给我吗? - samsam
@TaimurAjmal 也许你只是没有调用init方法?UIButton *BackButton = [[UIButton alloc] init]; - igrek

2
将 UIButton 拖到 UIToolBar 外部,进行格式化并分配选择器,然后将其拖入 UIToolBar 中。这对我有效!

0
我在导航栏中使用UIBarButton时遇到了同样的问题。 我使用了kaar3k提供的解决方案。感谢kaar3k。 在Storybaord中将UIButton拖出NavBar,然后进行格式化并分配选择器,最后将其拖到NavBar上即可!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接