使用背景图像自定义 UIBarbuttonitem

发布于 2024-09-06 17:55:29 字数 452 浏览 11 评论 0原文

我添加了一个 UIToolbar 实例和其顶部的按钮。每个按钮都属于 UIBarButtonItem 类。

我的要求是每个按钮都有一个自定义的布局,我不想使用苹果提供的原生按钮样式。所以我在 Interface Builder 中有 3 个选项(普通、边框、完成)。 我选择了“平原”样式,并选择了要添加为背景的图像 栏目->图像。

但它对我来说不起作用,使用普通选项让我更接近一点,因为边框和完成距离更近,但它仍然以白色轮廓显示图像。无论如何,是否添加了按钮上的图像,并且它们看起来完全一样。使用 UIButton 时这是一个非常简单的任务。我只是在 UIButton 的“属性”检查器中使用“图像”选项,然后选择我想要的图像。但在这里使用 UIBarButtonitem 时它根本不起作用。 这就是它的显示方式

谢谢 泰穆尔

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

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.

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

Thanks
Taimur

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌伤浅笑 2024-09-13 17:55:29

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

Taimur,

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];
}
清风疏影 2024-09-13 17:55:29

这里的问题是, UIBarButtonItem 不是 UIView 子类。
我也遇到了同样的问题,解决方案非常简单:

使用自定义背景图像创建一个 UIButton 对象,然后使用其 initWithCustomView: 方法创建 UIBarButtonItem 的实例,并将 UIButton 对象作为参数传递。

希望我能帮助你。

The problem here is, that UIBarButtonItem is not a UIView-Subclass.
I had the same problem too and the solution is quite trivial:

Create a UIButton Object with your custom background image, afterwards create an instance of a UIBarButtonItem using its initWithCustomView: method and passing your UIButton object as a parameter.

Hope I could help you.

自由范儿 2024-09-13 17:55:29

将一个 UIButton 拖放到 IB 中的 UIToolBar 之外,然后对其进行格式化并分配选择器并将其拖入 UIToolBar .,.,, 这对我有用!

Drag and Drop a UIButton out side the UIToolBar in IB then format it and assign the selectors and drag it into UIToolBar .,.,, This worked for me!!

扮仙女 2024-09-13 17:55:29

我在导航栏中的 UIBarButton 遇到了同样的问题。
我使用了kaar3k提供的解决方案。谢谢卡尔3k。
将 UIButton 拖放到 Storybaord 中的导航栏之外,然后对其进行格式化并分配选择器并将其拖动到导航栏上!

I had the same problem with a UIBarButton in the Navigation Bar.
I used the solution offered by kaar3k. Thanks kaar3k.
Drag and Drop a UIButton out side the NavBar in Storybaord then format it and assign the selectors and drag it onto the NavBar!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文