【问题标题】:Create view with width equal to the table view separator width创建宽度等于表格视图分隔符宽度的视图
【发布时间】:2017-02-09 20:01:35
【问题描述】:

我正在创建一个用自定义视图填充的表格视图单元格。我希望视图宽度与表格视图的分隔符插图相同(默认情况下文本单元格看起来像)。我的代码如下所示:

- (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;
};        

但这给了我一个完全填满整个屏幕的视图,而不仅仅是内部。有谁知道如何解决这个问题?

【问题讨论】:

    标签: ios cocoa uiview autolayout


    【解决方案1】:

    分隔符根据readableLayoutGuide定位。

    您想要相对于readableLayoutGuide leadingreadableLayoutGuide trailing 进行布局,而不是相对于centerXwidth 进行布局。

    【讨论】:

    • 完美,谢谢。为了记录你需要写.. toItem:self.contentView.readableContentGuide
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2012-12-18
    相关资源
    最近更新 更多