iOS:当左锚定为view.safeAreaLayoutGuide.leftAnchor时,UITableView不会加载单元格,但如果只是view.leftAnchor则将加载。

时间:2018-08-08 20:23:10

标签: ios uitableview nslayoutconstraint iphone-x safearealayoutguide

我有一个从屏幕左侧开始动画显示的视图控制器,该视图控制器有一个UITableView子视图。

在除一个设备(横向的iPhone X)上的所有设备和方向上,此表视图均正确显示。

在横向放置的iPhone X上,表格视图位于正确的位置,但是直到您滚动tableView时,它的单元格才会填充。滚动后,表格视图就好像一直都在那儿一样,但是其中有不可见的单元格。

此约束导致单元无法加载:

[tableView.leftAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leftAnchor].active = YES;

当用以下内容替换此约束时,问题也得到解决:

[tableView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;

我将UITableView的背景设置为红色,以表明它已正确放置,但是即使它们以纵向方向加载,也不会显示任何行。

enter image description here

我也尝试过将其保留为:

[tableView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;

但是这样做:

[tableView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];

解决了标头单元格的问题: header cells look correct

但不包含内容单元格: regular cells not indented

此代码会导致相同的问题:

[tableView setInsetsContentViewsToSafeArea:YES];

我正在像这样设置我的常规单元格:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if([reuseIdentifier isEqual: @"LayerCell"]) {
    _titleLabel = [[UILabel alloc] init];
    _imgView = [[UIImageView alloc] init];
    _toggleView = [[UIImageView alloc] init];

    [_imgView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self addSubview:_imgView];
    [_imgView.heightAnchor constraintEqualToAnchor:self.heightAnchor multiplier:0.8].active = YES;
    [_imgView.widthAnchor constraintEqualToAnchor:self.heightAnchor multiplier:0.8].active = YES;
    [_imgView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor].active = YES;
    [_imgView.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:8].active = YES;
    [_imgView setContentMode:UIViewContentModeScaleAspectFit];

    [self addSubview:_toggleView];
    [_toggleView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_toggleView.heightAnchor constraintEqualToAnchor:_imgView.heightAnchor].active = YES;
    [_toggleView.widthAnchor constraintEqualToAnchor:_imgView.widthAnchor].active = YES;
    [_toggleView.centerYAnchor constraintEqualToAnchor:_imgView.centerYAnchor].active = YES;
    [_toggleView.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:-10].active = YES;
    [_toggleView setContentMode:UIViewContentModeScaleAspectFit];

    [self addSubview:_titleLabel];
    [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_titleLabel.heightAnchor constraintEqualToConstant:20].active = YES;
    [_titleLabel.centerYAnchor constraintEqualToAnchor:_imgView.centerYAnchor].active = YES;
    [_titleLabel.rightAnchor constraintEqualToAnchor:_toggleView.leftAnchor constant:-10].active = YES;
    [_titleLabel.leftAnchor constraintEqualToAnchor:_imgView.rightAnchor constant:10].active = YES;
    [self layoutIfNeeded];
}
return self;
}

0 个答案:

没有答案