使用背景图片自定义UIBarbuttonItem [英] Customize UIBarbuttonitem with Background Images

查看:169
本文介绍了使用背景图片自定义UIBarbuttonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个UIToolbar实例和按钮。每个按钮属于UIBarButtonItem类。

I have added an instance of UIToolbar and buttons on top of it . Each button's belongs to the class of UIBarButtonItem.

我的要求是每个按钮有一个自定义的布局,我不想使用苹果提供的原生按钮样式。所以我有3个选项在Interface Builder(Plain,Bordered,Done)。
我选择了样式Plain,并选择了我想作为背景添加到
条形图 - >图像下的图像。

My Requirement is that each button has a customized layout , i dont want to use the native button styles provided by Apple. So i had 3 options in Interface Builder ( Plain , Bordered , Done ). I have selected the style Plain and selected the image i want to add as backgroun under Bar item -> Image.

但它did not work对我来说,使用plain选项让我有点接近,因为边框和完成都没有更近,但仍然以白色轮廓显示图像。有没有反正按钮上的图像被添加,他们看起来完全一样,因为他们是。它是一个相当简单的任务,同时使用UIButton。我只是使用图像选项在属性检查器中的UIButton和选择我想要的图像。但在这里,而使用UIBarButtonitem它不工作。
这是显示方式

But it didnt work for me, using the plain option gets me a little closer as border and done are nowhere closer but still it displays the images in white outline. Is there anyway that images on the buttons are added and they look exactly as they are. Its a pretty simple task while using UIButton. I just used Image option in the Attributes inspector for UIButton and select the image i want. But here while using UIBarButtonitem it doesnt work at all. This is how it displays

感谢
Taimur

Thanks Taimur

推荐答案

Taimur,

我碰到了你碰到的同样的问题。我们的设计师已经指定了在我们的应用程序的导航栏中的按钮的自定义外观。这是一个实用的方法,我写的生成UIBarButtonItems,我们需要,你应该能够修改它到你的需要:

I ran into the same problem that you're hitting. Our designer had specified a custom look for the buttons in our app's navigation bar. Here is a utility method that I wrote to generate the UIBarButtonItems that we needed, you should be able to modify it to your needs:

+ (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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆