2011-01-12 4 views
4

UIToolbar에 대한 두 가지 질문이 있습니다.컬러 이미지가 포함 된 버튼으로 UIToolbar를 사용자 정의하는 방법은 무엇입니까?

1 : UIToolbar에서 사용자 정의 이미지 (색상)와 함께 버튼을 사용하는 방법에 대한 수많은 Stackoverflow 응답을 읽었습니다. UIToolbar 상단에보기 (해킹)를 시도하고 이미지가 포함 된 단추를 넣으려고했지만 실패했습니다. 바로 지금 나는 붙어있다. 어떻게 이것을 할 수 있습니까?

2 : 동시에 많은 버튼을 "누른 상태"로 유지할 수 있습니까? 달성하고자하는 기능은 종류가 다른 종류의 정렬 버튼이 있습니다.

답변

3

두 번째 요구 사항에 대한 답변을 알고 있습니다.

IB에서보기를 클릭하고 검사관에서 여러 개의 터치 가능을 확인하십시오.

건배

8

대답 자체를 해결 좋아요 ... 여기있다 :

Can I have a UIBarButtonItem with a colored image?

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    toolbar = [[UIToolbar alloc] init]; 
    toolbar.barStyle = UIBarStyleDefault; 

    //Set the toolbar to fit the width of the app. 
    [toolbar sizeToFit]; 

    //Calculate the height of the toolbar 
    CGFloat toolbarHeight = [toolbar frame].size.height; 

    //Get the bounds of the parent view 
    CGRect rootViewBounds = self.parentViewController.view.bounds; 

    //Get the height of the parent view. 
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds); 

    //Get the width of the parent view, 
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds); 

    //Create a rectangle for the toolbar 
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight); 

    //Reposition and resize the receiver 
    [toolbar setFrame:rectArea]; 

    //Create a button 
    UIImage *image = [UIImage imageNamed:@"colorImage.png"]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.bounds = CGRectMake(0, 0, image.size.width, image.size.height);  
    [button setImage:image forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];  
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

    [toolbar setItems:[NSArray arrayWithObjects:barButtonItem,nil]]; 

    //Add the toolbar as a subview to the navigation controller. 
    [self.navigationController.view addSubview:toolbar]; 
} 

-(void)myAction{ 
    NSLog(@"jippiii"); 
} 
관련 문제