UITableViewRowAction with image instead of title

2019-04-07 15:33发布

问题:

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. enter image description here

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

回答1:

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).