Last active
November 16, 2017 07:02
-
-
Save asif-mistry/4d1550ca3fdc5cc7b492588e016ceeeb to your computer and use it in GitHub Desktop.
Make iPhone X design without using Safe Area Layout
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
//https://medium.com/@asifmistry/make-iphone-x-design-without-using-safe-area-layout-84c4bed7f60c | |
class func getSafeAreaInsets()->UIEdgeInsets | |
{ | |
if #available(iOS 11, *) { | |
let window = UIApplication.shared.windows[0] | |
let insets:UIEdgeInsets = window.safeAreaInsets | |
return insets | |
} | |
else { | |
return UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0) | |
} | |
//View controller | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
self.setupSafeLayout() | |
} | |
fileprivate func setupSafeLayout() { | |
let insets:UIEdgeInsets = Utility.getSafeAreaInsets() | |
self.mainViewTopConstraint.constant = insets.top | |
self.mainViewBottomConstraint.constant = insets.bottom | |
self.mainViewLeadingConstraint.constant = insets.left | |
self.mainViewTrailingConstraint.constant = insets.right | |
} | |
//Popup | |
let insets:UIEdgeInsets = Utility.getSafeAreaInsets() | |
popupView.translatesAutoresizingMaskIntoConstraints = false | |
mainView.addConstraint(NSLayoutConstraint(item: popupView, attribute: .trailing, relatedBy: .equal, toItem: mainView, attribute: .trailing, multiplier: 1.0, constant: -insets.right)) | |
mainView.addConstraint(NSLayoutConstraint(item: popupView, attribute: .top, relatedBy: .equal, toItem: mainView, attribute: .top, multiplier: 1.0, constant: insets.top)) | |
mainView.addConstraint(NSLayoutConstraint(item: popupView, attribute: .bottom, relatedBy: .equal, toItem: mainView, attribute: .bottom, multiplier: 1.0, constant: -insets.bottom)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment