How to configure UIToolbar with buttons containing color images?

I have two questions about UIToolbar:

1: I read a lot of Stackoverflow answers on how to use buttons with custom images (color) in UIToolbar. I tried to put the view (hack) on top of the UIToolbar and put buttons with images in it, but failed. I'm stuck right now. How can you do this?

2: Is there a way to have many buttons in the β€œpressed” state at the same time? The function I want to execute is different buttons with different types of sorting.

+4
source share
2 answers

It’s good that the answer was decided by myself ... here it is:

Is it possible to create a UIBarButtonItem with a color 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"); } 
+8
source

I know the answer to your second request.

in IB, click on the view, and in the inspector check the possibility of multiple touch.

Greetings

+3
source

Source: https://habr.com/ru/post/1335212/


All Articles