简体   繁体   English

创建宽度等于表格视图分隔符宽度的视图

[英]Create view with width equal to the table view separator width

I am creating a table view cell that I fill with a custom view. 我正在创建一个填充自定义视图的表视图单元格。 I would like the view width to be the same as the separator inset of the table view (as do text cell look like by default). 我希望视图宽度与表格视图的分隔符插入相同(默认情况下,文本单元格也是如此)。 My code looks like that: 我的代码如下所示:

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.preservesSuperviewLayoutMargins = true;
    self.contentView.preservesSuperviewLayoutMargins = true;

    myInnerView = [[MyInnerView alloc] initWithFrame:CGRectZero];

    [self.contentView addConstraint:[NSLayoutConstraint 
    constraintWithItem:myInnerView 
    attribute:NSLayoutAttributeCenterX
    relatedBy:NSLayoutRelationEqual
    toItem:self.contentView
    attribute:NSLayoutAttributeCenter:
    multiplier:1.0f constant:0.f]];

    [self.contentView addConstraint:[NSLayoutConstraint 
    constraintWithItem:myInnerView
    attribute:NSLayoutAttributeWidth
    relatedBy:NSLayoutRelationEqual
    toItem:self.contentView
    attribute:NSLayoutAttributeWidth
    multiplier:1.0f constant:0.f]];

    // some vertical constraints here
    }
return self;
};        

But this gives me a view that fills exactly the whole screen, not only the inner part. 但这给了我一个完全覆盖整个屏幕的视图,而不仅是内部部分。 Does anyone know how to solve this ? 有谁知道如何解决这个问题?

The separator is positioned based on readableLayoutGuide . 分隔符基于readableLayoutGuide定位。

Instead go laying out relative to centerX and width , you want to layout relative to the readableLayoutGuide leading and readableLayoutGuide trailing . 相反,相对于去铺出centerXwidth ,要布局相对readableLayoutGuide leadingreadableLayoutGuide trailing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM