Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewRowAction with image instead of title

I'd like to make a cell swipe action like mail app.

I set UIImage to backgroundColor of row action.

 action.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"remove"]];

But i get my image repeated side-by-side on the background. like this.

Is the image size problem? Could tell me how to fix it,or an other way to do it ?

like image 896
Daniel Avatar asked May 26 '16 02:05

Daniel


1 Answers

Yes, this is an image size problem. Even I had a similar requirement and faced the same problem. In this case when you use,

action.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"remove"]];

Even if you set the imageView.contentMode to:

UIViewContentModeScaleAspectFit
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFill

If the size of the image you are using and and the size of the button on the cell do not match, the image will not stretch to fill the entire button, rather the image pattern will just repeat itself until the entire area of the button is utilised. This is because you are setting the 'backgroundColor' and not the actual 'backgroundImage'. 'backgroundColor' unlike 'backgroundImage' does not adhere to UIContentMode of the button.

Hence, what you will have to do is, create a image which is exactly equal to the size of the button. Doing this is not possible if your cell has a dynamic height (height determined at runtime according to your content).

like image 160
7vikram7 Avatar answered Oct 03 '22 08:10

7vikram7