contentView不在iOS 6 UITableViewCell原型单元格中缩进 [英] contentView not indenting in iOS 6 UITableViewCell prototype cell

查看:126
本文介绍了contentView不在iOS 6 UITableViewCell原型单元格中缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Storyboard中的原型单元配置自定义 UITableViewCell 。但是,所有 UILabel (以及其他UI元素)似乎都没有添加到单元格的 contentView 中,而是被直接添加到 UITableViewCell 视图中。当单元格进入编辑模式时,这会产生问题,因为内容不会自动移位/缩进(如果它们位于 contentView 中,它会执行此操作)。 p>

使用Interface Builder / Storyboard / prototype布局单元格时,有没有办法将UI元素添加到 contentView 细胞?我找到的唯一方法是在代码中创建所有内容并使用 [cell.contentView addSubView:labelOne] 这不是很好,因为它更容易布局以图形方式显示单元格。

解决方案

进一步调查(查看单元格的子视图层次结构)Interface Builder确实将子视图放在单元格的 contentView ,它看起来不像。



问题的根本原因是iOS 6自动布局。当单元格进入编辑模式(并缩进)时, contentView 也会缩进,因此可以理解 contentView中的所有子视图将因 contentView 而移动(缩进)。但是,Interface Builder应用的所有自动布局约束似乎都与 UITableViewCell 本身相关,而不是 contentView 。这意味着即使 contentView 缩进,其中包含的子视图也不会 - 约束条件负责。



For例如,当我将 UILabel 放入单元格(并将其定位在距离单元格左侧10个点)时,IB会自动应用约束Horizo​​ntal Space(10) 。但是,此约束相对于 UITableViewCell 而不是 contentView 。这意味着当单元格缩进并且 contentView 移动时,标签保持不变,因为它符合约束以保持距离左侧10点的位置。 UITableViewCell



不幸的是(据我所知),没有办法删除这些IB创建的约束在IB本身,所以这就是我如何解决问题。



在单元格的 UITableViewCell 子类中,我为该约束创建了一个名为 cellLabelHSpaceConstraint IBOutlet 。您还需要 IBOutlet 作为标签本身,我称之为 cellLabel 。然后我按照以下方式实现了 -awakeFromNib 方法:

   - ( void)awakeFromNib {

// ------------------------------------ -------------------------------
//我们需要创建自己的约束,这对$有效b $ b // contentView,因此当单元格放入
//编辑模式时,UI元素会缩进
// ------------------ -------------------------------------------------

//删除IB添加的水平约束,因为对于单元格而言是有效的
//而不是contentView
[self removeConstraint:self.cellLabelHSpaceConstraint];

//创建一个字典来表示定位的视图
NSDictionary * labelViewDictionary = NSDictionaryOfVariableBindings(_cellLabel);

//创建新约束
NSArray * constraints = [NSLayoutConstraint constraintsWithVisualFormat:@| -10 - [_ cellLabel]options:0 metrics:nil views:labelViewDictionary];

//为contentView添加约束
[self.contentView addConstraints:constraints];

}

总之,上面将删除水平间距约束IB自动添加(对 UITableViewCell 而不是 contentView 有效)然后我们定义并添加我们自己的约束到 contentView



在我的情况下,所有其他 UILabels 是根据 cellLabel 的位置定位的,所以当我修复了这个元素的约束/定位时,所有其他元素都跟着并正确定位。但是,如果您的布局更复杂,那么您可能还需要为其他子视图执行此操作。


I am configuring a custom UITableViewCell using a prototype cell in a Storyboard. However, all the UILabels (and other UI elements) do not seem to be added to the cell's contentView, instead being added to the UITableViewCell view directly. This creates issues when the cell is put into editing mode, as the content is not automatically shifted/indented (which it would do, if they were inside the contentView).

Is there any way to add the UI elements to the contentView when laying out the cell using Interface Builder/Storyboard/prototype cells? The only way I have found is to create everything in code and use [cell.contentView addSubView:labelOne] which wouldn't be great, as it is much easier to layout the cell graphically.

解决方案

On further investigation (viewing the subview hierarchy of the cell) Interface Builder does place subviews within the cell's contentView, it just doesn't look like it.

The root cause of the issue was iOS 6 autolayout. When the cell is placed into editing mode (and indented) the contentView is also indented, so it stands to reason that all subviews within the contentView will move (indent) by virtue of being within the contentView. However, all the autolayout constraints applied by Interface Builder seem to be relative to the UITableViewCell itself, rather than the contentView. This means that even though the contentView indents, the subviews contained within do not - the constraints take charge.

For example, when I placed a UILabel into the cell (and positioned it 10 points from the left-hand side of the cell) IB automatically applied a constraint "Horizontal Space (10)". However, this constraint is relative to the UITableViewCell NOT the contentView. This means that when the cell is indented, and the contentView moves, the label stays put as it is complying with the constraint to remain 10 points from the left-hand side of the UITableViewCell.

Unfortunately (as far as I am aware) there is no way to remove these IB created constraints from within IB itself, so here is how I solved the problem.

Within the UITableViewCell subclass for the cell, I created an IBOutlet for that constraint called cellLabelHSpaceConstraint. You also need an IBOutlet for the label itself, which I called cellLabel. I then implemented the -awakeFromNib method as per below:

- (void)awakeFromNib {

    // -------------------------------------------------------------------
    // We need to create our own constraint which is effective against the
    // contentView, so the UI elements indent when the cell is put into
    // editing mode
    // -------------------------------------------------------------------

    // Remove the IB added horizontal constraint, as that's effective
    // against the cell not the contentView
    [self removeConstraint:self.cellLabelHSpaceConstraint];

    // Create a dictionary to represent the view being positioned
    NSDictionary *labelViewDictionary = NSDictionaryOfVariableBindings(_cellLabel);   

    // Create the new constraint
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[_cellLabel]" options:0 metrics:nil views:labelViewDictionary];

    // Add the constraint against the contentView
    [self.contentView addConstraints:constraints];

}

In summary, the above will remove the horizontal spacing constraint which IB automatically added (as is effective against the UITableViewCell rather than the contentView) and we then define and add our own constraint to the contentView.

In my case, all the other UILabels in the cell were positioned based upon the position of the cellLabel so when I fixed up the constraint/positioning of this element all the others followed suit and positioned correctly. However, if you have a more complex layout then you may need to do this for other subviews as well.

这篇关于contentView不在iOS 6 UITableViewCell原型单元格中缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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