Skip to content

Commit

Permalink
Merge pull request #1 from getcircuit/improve-autolayout-constraints
Browse files Browse the repository at this point in the history
Improve autolayout constraints
  • Loading branch information
thomasdelgado authored Apr 4, 2024
2 parents f615033 + d5f4e42 commit 3122e86
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions Source/Classes/OverlayContainerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ open class OverlayContainerViewController: UIViewController {

private var overlayContainerPortraitWidthConstraint: NSLayoutConstraint?
private var overlayContainerLandscapeWidthConstraint: NSLayoutConstraint?
private var overlayPositionConstraints: [NSLayoutConstraint] = []

private lazy var configuration = makeConfiguration()

Expand Down Expand Up @@ -224,11 +225,8 @@ open class OverlayContainerViewController: UIViewController {
translationHeightConstraint?.isActive = false
overlayContainerViewBottomConstraint?.isActive = false
overlayContainerViewStyleConstraint?.isActive = false
overlayTranslationView.removeFromSuperview()
overlayContainerView.removeFromSuperview()
NSLayoutConstraint.deactivate(overlayPositionConstraints)

overlayTranslationContainerView.addSubview(overlayTranslationView)
overlayTranslationView.addSubview(overlayContainerView)
setOverlayPosition()
setOverlayConstraints()
loadTranslationController()
Expand All @@ -245,24 +243,43 @@ open class OverlayContainerViewController: UIViewController {
overlayTranslationContainerView.pinToSuperview()
overlayTranslationContainerView.addSubview(overlayTranslationView)
overlayTranslationView.addSubview(overlayContainerView)
overlayTranslationView.translatesAutoresizingMaskIntoConstraints = false
overlayContainerView.translatesAutoresizingMaskIntoConstraints = false
setOverlayPosition()
setOverlayConstraints()
loadTranslationController()
}

private func setOverlayPosition() {
var translationConstraints: [NSLayoutConstraint] = [
overlayTranslationView.leftAnchor.constraint(equalTo: overlayTranslationContainerView.leftAnchor),
overlayTranslationView.rightAnchor.constraint(equalTo: overlayTranslationContainerView.rightAnchor)
]

var overlayConstraints: [NSLayoutConstraint] = [
overlayContainerView.topAnchor.constraint(equalTo: overlayTranslationView.topAnchor),
overlayContainerView.leftAnchor.constraint(equalTo: overlayTranslationView.leftAnchor),
overlayContainerView.rightAnchor.constraint(equalTo: overlayTranslationView.rightAnchor)
]

if landscapeLayout {
if UIDevice.current.userInterfaceIdiom == .phone {
overlayTranslationView.pinToSuperview(edges: [.all])
translationConstraints.append(overlayContainerView.topAnchor.constraint(equalTo: overlayTranslationContainerView.topAnchor))
translationConstraints.append(overlayContainerView.bottomAnchor.constraint(equalTo: overlayTranslationContainerView.bottomAnchor))
} else {
overlayTranslationView.pinToSuperview(edges: [.top, .left, .right])
translationConstraints.append(overlayContainerView.topAnchor.constraint(equalTo: overlayTranslationContainerView.topAnchor))
}

overlayContainerView.pinToSuperview(edges: [.left, .top, .right, .bottom])
overlayConstraints.append(overlayContainerView.bottomAnchor.constraint(equalTo: overlayTranslationView.bottomAnchor))

} else {
overlayTranslationView.pinToSuperview(edges: [.bottom, .left, .right])
overlayContainerView.pinToSuperview(edges: [.left, .top, .right])
translationConstraints.append(
overlayContainerView.bottomAnchor.constraint(equalTo: overlayTranslationContainerView.bottomAnchor)
)
}

overlayPositionConstraints = translationConstraints + overlayConstraints
NSLayoutConstraint.activate(overlayPositionConstraints)
}

private func setOverlayConstraints() {
Expand Down

0 comments on commit 3122e86

Please sign in to comment.