2010-07-21 5 views
1

UIToolbar에서 사용해야하는 UIBarButtonItem에 이미지를 추가하려고합니다.UIToolbar의 UIBarButtonItem에 이미지 추가

이미지를 읽고 심지어 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]; 

귀하의 도움을 많이 주시면 감사하겠습니다.

감사합니다! 일반적 UIKit 물건에 기대하는 것보다

답변

3

다른

Shumais UL 하크, 당신은 명시 적으로 버튼 프레임을 설정해야 할 수도 있습니다. 어쩌면 그건 네 문제 야.

이것은 사용자 지정 스타일의 뒤로 버튼에 대해 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); 나는이 일을하기 위해 막 정신을 잃었습니다. 나는 이제까지 Button-Frame 관계를 잊을 것이라고 생각하지 않는다 !! 다시 한번 감사드립니다. –