UITableViewRowAction with image instead of title

时间秒杀一切 提交于 2019-12-21 12:17:50

问题


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



来源:https://stackoverflow.com/questions/37450555/uitableviewrowaction-with-image-instead-of-title

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