2011-01-12 55 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"); 
}