我正在创建一个表格 View 单元格,并用自定义 View 填充该单元格。我希望 View 宽度与 TableView 的分隔符插入相同(默认情况下文本单元格看起来也是如此)。我的代码如下所示:
- (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;
};
但这给了我一个完全填充整个屏幕的 View ,而不仅仅是内部部分。有谁知道如何解决这个问题吗?
最佳答案
分隔符基于readableLayoutGuide
定位。
而不是相对于 centerX
和 width
进行布局,您想要相对于 leavedLayoutGuide
leading
进行布局和可读布局指南
尾随
。
关于ios - 创建宽度等于 TableView 分隔符宽度的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42145802/