【问题标题】:add image to UIBarButtonItem for UIToolbar为 UIToolbar 添加图像到 UIBarButtonItem
【发布时间】:2010-07-21 13:38:05
【问题描述】:

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

我已经设法读取图像,甚至让它与 UIImageView 一起显示,但是当我将它添加到 UIBarButtonItem 然后将该项目添加到 UIToolbar 时,工具栏只会取代“空白”空间的大小和我正在尝试加载的图像的形状。

这是我正在尝试的。

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

您的帮助将不胜感激。

谢谢!

Shumais Ul Haq

【问题讨论】:

    标签: iphone uibarbuttonitem uitoolbar


    【解决方案1】:

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

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

    请注意,这用于导航栏,而不是工具栏,但我认为机制是相同的,因为它也是 UIBarButtonItem。对于UIToolbar,您只需在编译时使用 IB 即可。

    #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)];
    

    【讨论】:

    • 谢谢mvds!!!对,就是框架!!我只是添加了这一行 button2.frame = CGRectMake(0, 0, image.size.width, image.size.height);我花了很多时间试图让它发挥作用。我不认为我会忘记 Button-Frame 关系!再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    相关资源
    最近更新 更多