如何将 UITableView 单元格子视图正确定位到右侧? [英] How to properly position a UITableView cell subview to the right?

查看:18
本文介绍了如何将 UITableView 单元格子视图正确定位到右侧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在右边的表格单元格中添加一个 UISwitch.我首先尝试 将其添加到 accesoryView,但是当我在表中,UISwitch 消失了.所以现在我将它添加到 contentView,这是有效的很好,没有消失.但它将它添加到左上角.

So I'm trying to add a UISwitch to my table cells on the right. I first tried adding it to the accesoryView, but when I entered to editing mode in the table, the UISwitch dissapeared. So now I added it to the contentView, which is working great, no dissapearing. But it adds it to the top left corner.

所以我试图将它添加到右侧,但我想远离绝对值以支持所有尺寸和方向.

So I'm trying to add it to the right but I want to stay away of absolute values in order to support all sizes and orientations.

所以我正在尝试使用此代码:

So I'm trying with this code:

[[cell contentView] addSubview:switchView];
switchView.frame = CGRectMake(cell.contentView.frame.size.width - 
                                    switchView.frame.size.width, 
                              0,
                              switchView.frame.size.width, 
                              switchView.frame.size.height);

但这就是我得到的:

有什么想法可以解决这个问题吗?

Any ideas how can I fix this?

推荐答案

您需要在开关上设置适当的自动调整大小蒙版或布局约束.

You need to set the appropriate autoresizing masks or layout constraints on the switch.

如果您使用的是弹簧和支柱,您希望开关仅具有灵活的左边距.这将使其与您的超级视图的右侧边缘保持固定距离 - 在您添加开关和它出现在屏幕上之间,内容视图很可能会调整大小.

If you're using springs and struts, you want the switch to have a flexible left margin only. This will keep it a fixed distance from the right hand edge of your superview - the content view is very likely resized between you adding the switch and it appearing on the screen.

要设置此掩码:

switchView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 

如果您使用自动布局(这使得不使用特定的帧值非常容易!),您需要将开关固定到超级视图的右边缘 - [switch]-| in视觉格式.

If you're using Auto layout (which makes not using specific frame values very easy!), you need to pin the switch to the right edge of the superview - [switch]-| in visual format.

这篇关于如何将 UITableView 单元格子视图正确定位到右侧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆