2
  1. 我已經創建了兩個UIView一個headerview和第二是UICollectionViewUIScrollView兩個子視圖和我有隱藏我的NavigationBar整個應用程序。Xcode版本爲9,iOS11並且工作正常,但iPhone x除外。 UI得到了倒塌

  2. 現在我已經添加以下代碼集「SafeAreaLayoutGuides」在iOS11但在這裏應用粗魯由於上海華發現。

我已經添加下面的代碼。

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]; 
+2

代碼在哪裏? –

+0

對不起...我剛剛更新。@ RonakThakkar –

+0

我在iOS11中遇到了一些問題。 –

回答

1

如果parentView是nil(因爲self.view.superviewnil),那麼這意味着self.view尚未添加到視圖層級結構尚未。

確保無論包含此視圖的視圖都稱爲self.addSubview(childView);這將修復「nil superview」問題。

相關問題