2
  1. 我已经创建了两个第UIView一个headerview和第二个UICollectionView都是子视图,UIScrollView并且我隐藏了我的NavigationBar整个应用程序。

  2. 现在,我在 iOS11 中为设置“SafeAreaLayoutGuides”添加了以下代码,但由于发现 superview nil,此处的应用程序 糟糕

我在下面添加了代码。

    UIView *parentView = self.view.superview;
    UIView *childView = scrollViewMain.superview;
    childView.translatesAutoresizingMaskIntoConstraints = NO;

    NSLayoutConstraint *topConstraint;
    NSLayoutConstraint *bottomConstraint;

    if (@available(iOS 11, *)) {
        topConstraint   = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parentView.safeAreaLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];

        bottomConstraint   = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parentView.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    } else {
        topConstraint   = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];

        bottomConstraint = [NSLayoutConstraint constraintWithItem:childView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    }

    [parentView addConstraint:topConstraint];
    [parentView addConstraint:bottomConstraint];
4

1 回答 1

1

如果parentView为 nil(因为self.view.superviewis nil),则表示self.view尚未将其添加到视图层次结构中。

确保包含该视图的任何视图都已调用self.addSubview(childView);这将解决“ nilsuperview”问题。

于 2017-11-02T19:57:28.203 回答