Resize UITableViewCell content when delete button shows up

▼魔方 西西 提交于 2019-12-18 05:12:10

问题


Is there any way to use autoresizing masks to move my content so that the delete button doesn't cover it up? Googling has told me that I need to set an autoresizing mask of UIViewAutoresizingFlexibleRightMargin on my subview. It seems to me like UIViewAutoresizingFlexibleWidth would actually make more sense; though I've tried them both and neither works.

The view that I am trying to shrink is just a label that is a subview of the cell's contentView. I am unsure if the contentView itself automatically resizes when the delete button shows up; but it seems like it isn't; otherwise my autoresizing mask should have worked.

If the presence of the delete button doesn't cause any views to be resized; is there anyway that I can do this manually?


回答1:


You should use UIViewAutoresizingFlexibleLeftMargin.

Here's why. You want your contents to move to the left, basically making it seem like the delete button is pushing the contents to the left, out of it's way. flexibleLeftMargin basically means your UILabel will stay fixed to the right side of your contentView. The reason you want this, is because the delete button actually causes your contentView to shrink it's width.

The autoresizingmask of your UILabel refers to how it behaves inside the contentView, not the cell.

Give it a try, it should work.




回答2:


This question is really old but I feel I should answer this anyway since I just found the solution myself.

Only the cell's ContentView gets resized with the confirmation button is shown. If you don't add your views (labels, imageviews, etc...) to the cell.contentView instead of adding them to the cell directly then they won't be resized when the contentView is resized. In my case, I was adding it to the cell directly.

So, instead of doing something like:

UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, width-10, 20)];
[nameLabel setFont:[UIFont boldSystemFontOfSize:16]];
[nameLabel setHighlightedTextColor:[UIColor whiteColor]];
[nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[nameLabel setTag:101];
[cell addSubview:nameLabel];
[nameLabel release];

you should do:

UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, width-10, 20)];
[nameLabel setFont:[UIFont boldSystemFontOfSize:16]];
[nameLabel setHighlightedTextColor:[UIColor whiteColor]];
[nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[nameLabel setTag:101];
[[cell contentView] addSubview:nameLabel]; // <<--- note the change in this line!
[nameLabel release];

Hope this helps others who stumble upon this issue.




回答3:


I am using iOS 7, I got the same issue. I am using a separate xib for the UITableViewCell with auto layout enabled, so just added one more constraint to the label so that it will have a fixed gap on its right side.



来源:https://stackoverflow.com/questions/4390583/resize-uitableviewcell-content-when-delete-button-shows-up

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