当删除按钮出现时调整 UITableViewCell 内容的大小

发布于 2024-10-07 00:47:38 字数 358 浏览 7 评论 0原文

有什么方法可以使用自动调整大小蒙版来移动我的内容,以便删除按钮不会覆盖它?谷歌搜索告诉我,我需要在子视图上设置 UIViewAutoresizingFlexibleRightMargin 的自动调整大小掩码。在我看来,UIViewAutoresizingFlexibleWidth实际上更有意义;虽然我已经尝试过它们,但都不起作用。

我试图缩小的视图只是一个标签,它是单元格 contentView 的子视图。我不确定当删除按钮出现时 contentView 本身是否会自动调整大小;但似乎并非如此;否则我的自动调整大小蒙版应该可以工作。

如果删除按钮的存在不会导致任何视图大小调整;无论如何,我可以手动执行此操作吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-10-14 00:47:38

您应该使用 UIViewAutoresizingFlexibleLeftMargin。

原因如下。您希望内容向左移动,基本上使删除按钮看起来像是将内容推到左侧,使其脱离原来的位置。 flexibleLeftMargin 基本上意味着您的 UILabel 将固定在 contentView 的右侧。您想要这个的原因是因为删除按钮实际上会导致您的 contentView 缩小其宽度。

UILabel 的 autoresizingmask 指的是它在 contentView(而不是单元格)内的行为方式。

尝试一下,应该会有效。

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.

酒中人 2024-10-14 00:47:38

这个问题确实很老了,但我觉得无论如何我都应该回答这个问题,因为我自己刚刚找到了解决方案。

仅单元格的 ContentView 会调整大小并显示确认按钮。如果您不将视图(标签、图像视图等)添加到 cell.contentView 中,而不是直接将它们添加到单元格中,那么当 contentView 调整大小时,它们不会调整大小。就我而言,我将其直接添加到单元格中。

因此,不要做类似的事情:

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];

你应该做:

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];

希望这可以帮助其他偶然发现这个问题的人。

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.

乖乖哒 2024-10-14 00:47:38

我使用的是 iOS 7,也遇到了同样的问题。我正在为启用了自动布局的 UITableViewCell 使用单独的 xib,因此只需向标签添加一个约束,以便它在右侧有一个固定的间隙。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文