为UIToolbar添加图像到UIBarButtonItem [英] add image to UIBarButtonItem for UIToolbar

查看:111
本文介绍了为UIToolbar添加图像到UIBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像添加到UIBarButtonItem中,我必须在UIToolbar中使用它。

I am trying to add an image to the UIBarButtonItem which I have to use in a UIToolbar.

我已经设法读取图像甚至让它显示使用UIImageView,但是当我将它添加到UIBarButtonItem然后将该项添加到UIToolbar时,工具栏只是将空白空格替换为我尝试加载的图像的大小和形状。

I have managed to read the image and even get it to display with a UIImageView, but when i add it to the UIBarButtonItem and then add that item to the UIToolbar, the toolbar just displaces a "Blank White" space the size and shape of the image that I am trying to load.

这是我正在尝试的。

UIImage *image = [UIImage imageNamed:@"6.png"];

//This is the UIImageView that I was using to display the image so that i know that it is being read from the path specified.  
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 50, image.size.width, image.size.height);
[self.view addSubview:imageView];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:image forState:UIControlStateNormal];

//This is the first way that I was trying to accomplish the task but i just get a blank white space

//This is the Second way but with the same blank white result.
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithCustomView:button2];

NSArray *items = [NSArray arrayWithObjects: systemItem1, nil];

//Adding array of buttons to toolbar
[toolBar setItems:items animated:NO];

//Adding the Toolbar to the view.
[self.view addSubview:toolBar];

我们非常感谢您的帮助。

Your help will be much appreciated.

谢谢!

Shumais Ul Haq

Shumais Ul Haq

推荐答案

除了你通常在UIKit的东西中,你可能需要明确地为按钮设置一个框架。也许这就是你的问题。

Other than you'd normally expect in UIKit stuff, you might need to set a frame for the button explicitly. Maybe that's your problem.

这是我为自定义样式的后退按钮编写的,作为 UIBarButtonItem 的类别(但你可以从中获取所需的部分)。

This is what I wrote for a custom styled back button, as a category to UIBarButtonItem (but you can just take the pieces you need from it).

请注意,这用于导航栏,而不是工具栏,但我认为机制是同样,因为它也是 UIBarButtonItem 。对于 UIToolbar ,你可以在编译时使用IB来实现它。

Note that this was used for the navigation bar, not the toolbar, but I presume the mechanics are the same, since it is a UIBarButtonItem as well. For UIToolbar you can just use IB to get it right at compile time.

#define TEXT_MARGIN 8.0f
#define ARROW_MARGIN 12.0f
#define FONT_SIZE 13.0f
#define IMAGE_HEIGHT 31.0f

+(UIBarButtonItem*)arrowLeftWithText:(NSString*)txt target:(id)target action:(SEL)selector
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [[UIImage imageNamed:@"arrow_left.png"]
        stretchableImageWithLeftCapWidth:15 topCapHeight:0];

    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchDown];

    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [btn setContentEdgeInsets:UIEdgeInsetsMake(0.0f,0.0f,0.0f,TEXT_MARGIN)];
    [btn.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FONT_SIZE]];
    [btn.titleLabel setShadowOffset:CGSizeMake(0.0f,-1.0f)];

    /**** this is the magic line ****/
    btn.frame = CGRectMake(0.0f,0.0f,
        [txt sizeWithFont:[btn.titleLabel font]].width+ARROW_MARGIN+TEXT_MARGIN,
        IMAGE_HEIGHT);

    [btn styleBarButtonForState:UIControlStateNormal withImage:img andText:txt];
    [btn styleBarButtonForState:UIControlStateDisabled withImage:img andText:txt];
    [btn styleBarButtonForState:UIControlStateHighlighted withImage:img andText:txt];
    [btn styleBarButtonForState:UIControlStateSelected withImage:img andText:txt];
    return [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
}

用法:

[UIBarButtonItem arrowLeftWithText:@"Back" target:self action:@selector(dismiss)];

这篇关于为UIToolbar添加图像到UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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