2011-01-12 6 views
4

私はUIToolbarに関する2つの質問があります:カラー画像を含むボタンでUIToolbarをカスタマイズするには?

1:UIToolbarでカスタムイメージ(色付き)のボタンを使用する方法について、多数のStackoverflowの回答を読んでいます。私はUIToolbarの上にビュー(ハック)を置いてボタンをイメージに入れようとしましたが失敗しました。今、私は立ち往生しています。どのようにこれを達成することができますか?

2:多くのボタンを同時に「押した」状態にする方法はありますか?達成したい機能は、異なる種類の並べ替えを持つ異なるボタンです。

答えて

3

私はあなたの第2の要件の答えを知っています。

IBのビューをクリックし、インスペクタで複数のタッチを有効にしてください。

乾杯

8

答えは自分自身を解決し[OK]を...ここにある:

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"); 
} 
関連する問題