Created
October 17, 2017 03:38
-
-
Save ha1f/e9eabb2985a433de273ab6a3c07d5e04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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