2010-07-21 108 views
1

我想添加圖像到UIBarButtonItem,我必須在UIToolbar中使用。將圖像添加到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哈克

回答

3

除了你通常希望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)]; 
+0

謝謝MVDS! 是的,這是框架! 我簡單地加入此線 button2.frame = CGRectMake(0,0,image.size.width,image.size.height); 我花了大量的時間試圖讓這個工作。我不認爲我會忘記按鈕框架的關係! 再次感謝! – 2010-07-21 14:03:57

相關問題