Skip to content

Instantly share code, notes, and snippets.

@ha1f
Created October 17, 2017 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ha1f/e9eabb2985a433de273ab6a3c07d5e04 to your computer and use it in GitHub Desktop.
Save ha1f/e9eabb2985a433de273ab6a3c07d5e04 to your computer and use it in GitHub Desktop.
extension UIViewController {
private class ExtendedBottomView: UIView { }
/// Create bottom View which covers from bottom of the screen to the
/// bottom edge of the bottomAccessoryView.
/// (useful on iPhone X only)
/// Make sure not to call more than once (typically, on viewDidLoad),
/// and bottomAccessoryView has added as subview.
///
/// - parameter bottomAccessoryView: a target View to decide the
/// top-edge of bottomView
func addBottomView(to bottomAccessoryView: UIView) {
guard let bottomSuperView = bottomAccessoryView.superview,
let view = view,
bottomSuperView == view else {
return
}
guard !view.subviews.contains(where: { $0 is ExtendedBottomView }) else {
assertionFailure("View cannot be added more than once")
return
}
let bottomView = ExtendedBottomView()
view.addSubview(bottomView)
bottomView.translatesAutoresizingMaskIntoConstraints = false
bottomView.backgroundColor = bottomAccessoryView.backgroundColor
NSLayoutConstraint.activate([
bottomView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
bottomView.leftAnchor.constraint(equalTo: view.leftAnchor),
bottomView.rightAnchor.constraint(equalTo: view.rightAnchor),
bottomView.topAnchor.constraint(equalTo: bottomAccessoryView.bottomAnchor)
])
bottomView.setNeedsLayout()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment