Skip to content

Commit

Permalink
Improve autolayout constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasdelgado committed Apr 3, 2024
1 parent f615033 commit bf000d8
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 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 @@ -251,18 +249,35 @@ open class OverlayContainerViewController: UIViewController {
}

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.topAnchor.constraint(equalTo: overlayTranslationContainerView.topAnchor)
)
}

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

private func setOverlayConstraints() {
Expand Down

0 comments on commit bf000d8

Please sign in to comment.