如何将Xib粘贴到IphoneX的顶部

时间:2018-04-20 14:03:54

标签: swift xcode uiview xib iphone-x

我创建了一个Xib文件并将其与UIView子类链接。

我使用下面的代码:

@IBOutlet var contentView: UIView!

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

private func commonInit() {
    Bundle.main.loadNibNamed("SmartRemoteConnectedBanner", owner: self, options: nil)

    contentView.heightAnchor.constraint(equalToConstant: 300).isActive = true
    addSubview(contentView)
    contentView.frame = self.bounds
    contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}

它与标准iPhone完美配合,但我无法将其粘贴在IphoneX的顶部。

1 个答案:

答案 0 :(得分:0)

您需要为superview设置约束,而不是在iPhoneX中设置较小的SafeArea。您还可以扩展SafeArea。 Apple guide

var newSafeArea = UIEdgeInsets()
   // Adjust the safe area to accommodate 
   //  the height of the bottom view.
   if let bottomViewHeight = bottomView?.bounds.size.height {
      newSafeArea.bottom += bottomViewHeight
   }
   // Adjust the safe area insets of the 
   //  embedded UIView.
   contentView.additionalSafeAreaInsets = newSafeArea
}