How to customize the UIToolbar with buttons that contains colored images?

杀马特。学长 韩版系。学妹 提交于 2019-12-22 08:44:28

问题


I have two questions about the UIToolbar:

1: I have read numerous Stackoverflow answers on how to use buttons with custom images (colored) in a UIToolbar. I have tried to put a view (hack) on top of the UIToolbar and put the buttons with the images in it, but failed. Right now I am stuck. How can you accomplish this?

2: Is there a way to have many buttons in "pressed" state simultaneously? The function I want to accomplish is different buttons with different kind kinds of sorting.


回答1:


Ok the answer solved itself... here it is:

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");
}



回答2:


I know the answer of your 2nd requirement.

in the IB click on the view and in inspector check multiple touch enable.

Cheers



来源:https://stackoverflow.com/questions/4666726/how-to-customize-the-uitoolbar-with-buttons-that-contains-colored-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!