From d217c3ad2b4e6d7a30f8ac598f2dbb0140e674ff Mon Sep 17 00:00:00 2001 From: Shin Yamamoto Date: Sat, 6 Jun 2020 16:40:39 +0900 Subject: [PATCH] Rename heightConstraint to staticConstraint --- .../Sources/FloatingPanelController.swift | 6 +-- Framework/Sources/FloatingPanelLayout.swift | 50 ++++++++++++------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/Framework/Sources/FloatingPanelController.swift b/Framework/Sources/FloatingPanelController.swift index dcc32cc..2efdf45 100644 --- a/Framework/Sources/FloatingPanelController.swift +++ b/Framework/Sources/FloatingPanelController.swift @@ -341,7 +341,7 @@ open class FloatingPanelController: UIViewController { if #available(iOS 11.0, *) { // Ensure the panel's static constraint after rotating a device in static mode if contentMode == .static { - floatingPanel.layoutAdapter.updateHeight() + floatingPanel.layoutAdapter.updateStaticConstraint() } } else { // Because {top,bottom}LayoutGuide is managed as a view @@ -432,7 +432,7 @@ open class FloatingPanelController: UIViewController { contentOffset = scrollView?.contentOffset } - floatingPanel.layoutAdapter.updateHeight() + floatingPanel.layoutAdapter.updateStaticConstraint() if let contentOffset = contentOffset { scrollView?.contentOffset = contentOffset @@ -455,7 +455,7 @@ open class FloatingPanelController: UIViewController { contentOffset = scrollView?.contentOffset } - floatingPanel.layoutAdapter.updateHeight() + floatingPanel.layoutAdapter.updateStaticConstraint() floatingPanel.layoutAdapter.activateLayout(for: floatingPanel.state, forceLayout: forceLayout) if let contentOffset = contentOffset { diff --git a/Framework/Sources/FloatingPanelLayout.swift b/Framework/Sources/FloatingPanelLayout.swift index b8fd785..e2a6dcb 100644 --- a/Framework/Sources/FloatingPanelLayout.swift +++ b/Framework/Sources/FloatingPanelLayout.swift @@ -287,7 +287,7 @@ class FloatingPanelLayoutAdapter { private(set) var interactionEdgeConstraint: NSLayoutConstraint? private(set) var animationEdgeConstraint: NSLayoutConstraint? - private var heightConstraint: NSLayoutConstraint? + private var staticConstraint: NSLayoutConstraint? private var activeStates: Set { return Set(layout.stateAnchors.keys) @@ -788,10 +788,10 @@ class FloatingPanelLayoutAdapter { // The method is separated from prepareLayout(to:) for the rotation support // It must be called in FloatingPanelController.traitCollectionDidChange(_:) - func updateHeight() { + func updateStaticConstraint() { guard let vc = vc else { return } - NSLayoutConstraint.deactivate(constraint: heightConstraint) - heightConstraint = nil + NSLayoutConstraint.deactivate(constraint: staticConstraint) + staticConstraint = nil if vc.contentMode == .fitToBounds { surfaceView.containerOverflow = 0 @@ -800,30 +800,42 @@ class FloatingPanelLayoutAdapter { let anchor = layout.stateAnchors[self.edgeMostState]! if anchor is FloatingPanelIntrinsicLayoutAnchor { - let heightMargin: CGFloat + var constant = layout.anchorPosition.mainDimension(surfaceView.intrinsicContentSize) switch layout.anchorPosition { - case .bottom: - heightMargin = safeAreaInsets.bottom case .top: - heightMargin = safeAreaInsets.top + if anchor.referenceGuide == .safeArea { + constant += safeAreaInsets.top + } + staticConstraint = surfaceView.heightAnchor.constraint(equalToConstant: constant) + case .left: + if anchor.referenceGuide == .safeArea { + constant += safeAreaInsets.left + } + staticConstraint = surfaceView.widthAnchor.constraint(equalToConstant: constant) + case .bottom: + if anchor.referenceGuide == .safeArea { + constant += safeAreaInsets.bottom + } + staticConstraint = surfaceView.heightAnchor.constraint(equalToConstant: constant) + case .right: + if anchor.referenceGuide == .safeArea { + constant += safeAreaInsets.right + } + staticConstraint = surfaceView.widthAnchor.constraint(equalToConstant: constant) } - let constant: CGFloat - switch anchor.referenceGuide { - case .safeArea: - constant = surfaceView.intrinsicContentSize.height + heightMargin - case .superview: - constant = surfaceView.intrinsicContentSize.height - } - heightConstraint = surfaceView.heightAnchor.constraint(equalToConstant: constant) } else { switch layout.anchorPosition { case .top: - heightConstraint = surfaceView.heightAnchor.constraint(equalToConstant: positionY(for: self.directionalMostState)) + staticConstraint = surfaceView.heightAnchor.constraint(equalToConstant: position(for: self.directionalMostState)) + case .left: + staticConstraint = surfaceView.widthAnchor.constraint(equalToConstant: position(for: self.directionalMostState)) case .bottom: - heightConstraint = vc.view.heightAnchor.constraint(equalTo: surfaceView.heightAnchor, constant: positionY(for: self.directionalLeastState)) + staticConstraint = vc.view.heightAnchor.constraint(equalTo: surfaceView.heightAnchor, constant: position(for: self.directionalLeastState)) + case .right: + staticConstraint = vc.view.heightAnchor.constraint(equalTo: surfaceView.widthAnchor, constant: position(for: self.directionalLeastState)) } } - NSLayoutConstraint.activate(constraint: heightConstraint) + NSLayoutConstraint.activate(constraint: staticConstraint) surfaceView.containerOverflow = vc.view.bounds.height }