From cf70929204963b873cf667ecda2fcbc3fa506076 Mon Sep 17 00:00:00 2001 From: Sven Tiigi Date: Sat, 1 Jun 2019 06:46:18 +0200 Subject: [PATCH] Added ContentInset Property on SurfaceView API (#200) * Added Show ContentInset to Example application --- Examples/Samples/Sources/ViewController.swift | 15 ++++++ .../Sources/FloatingPanelSurfaceView.swift | 51 +++++++++++++++---- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/Examples/Samples/Sources/ViewController.swift b/Examples/Samples/Sources/ViewController.swift index 2154bc5..b07b97d 100644 --- a/Examples/Samples/Sources/ViewController.swift +++ b/Examples/Samples/Sources/ViewController.swift @@ -23,6 +23,7 @@ class SampleListViewController: UIViewController { case showNestedScrollView case showRemovablePanel case showIntrinsicView + case showContentInset var name: String { switch self { @@ -36,6 +37,7 @@ class SampleListViewController: UIViewController { case .showNestedScrollView: return "Show Nested ScrollView" case .showRemovablePanel: return "Show Removable Panel" case .showIntrinsicView: return "Show Intrinsic View" + case .showContentInset: return "Show with ContentInset" } } @@ -51,6 +53,7 @@ class SampleListViewController: UIViewController { case .showNestedScrollView: return "NestedScrollViewController" case .showRemovablePanel: return "DetailViewController" case .showIntrinsicView: return "IntrinsicViewController" + case .showContentInset: return nil } } } @@ -296,6 +299,18 @@ extension SampleListViewController: UITableViewDelegate { fpc.isRemovalInteractionEnabled = true self.present(fpc, animated: true, completion: nil) + + case .showContentInset: + let contentViewController = UIViewController() + contentViewController.view.backgroundColor = .green + + let fpc = FloatingPanelController() + fpc.set(contentViewController: contentViewController) + fpc.surfaceView.contentInsets = .init(top: 20, left: 20, bottom: 0, right: 20) + + fpc.delegate = self + fpc.isRemovalInteractionEnabled = true + self.present(fpc, animated: true, completion: nil) default: detailPanelVC?.removePanelFromParent(animated: true, completion: nil) mainPanelVC?.removePanelFromParent(animated: true) { diff --git a/Framework/Sources/FloatingPanelSurfaceView.swift b/Framework/Sources/FloatingPanelSurfaceView.swift index 5805d8e..4ef1cb1 100644 --- a/Framework/Sources/FloatingPanelSurfaceView.swift +++ b/Framework/Sources/FloatingPanelSurfaceView.swift @@ -34,6 +34,16 @@ public class FloatingPanelSurfaceView: UIView { /// A root view of a content view controller public weak var contentView: UIView! + + /// The content insets specifying the insets around the content view. + /// + /// - important: Currently the `bottom` inset is ignored. + public var contentInsets: UIEdgeInsets = .zero { + didSet { + // Needs update constraints + self.setNeedsUpdateConstraints() + } + } private var color: UIColor? = .white { didSet { setNeedsLayout() } } var bottomOverflow: CGFloat = 0.0 // Must not call setNeedsLayout() @@ -91,12 +101,19 @@ public class FloatingPanelSurfaceView: UIView { private lazy var containerViewTopInsetConstraint: NSLayoutConstraint = containerView.topAnchor.constraint(equalTo: topAnchor, constant: containerTopInset) private lazy var containerViewHeightConstraint: NSLayoutConstraint = containerView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1.0) + + /// The content view top constraint + private var contentViewTopConstraint: NSLayoutConstraint? + /// The content view left constraint + private var contentViewLeftConstraint: NSLayoutConstraint? + /// The content right constraint + private var contentViewRightConstraint: NSLayoutConstraint? + /// The content height constraint + private var contentViewHeightConstraint: NSLayoutConstraint? - private lazy var contentViewHeightConstraint: NSLayoutConstraint? = nil - - private lazy var grabberHandleWidthConstraint: NSLayoutConstraint! = grabberHandle.widthAnchor.constraint(equalToConstant: grabberHandleWidth) - private lazy var grabberHandleHeightConstraint: NSLayoutConstraint! = grabberHandle.heightAnchor.constraint(equalToConstant: grabberHandleHeight) - private lazy var grabberHandleTopConstraint: NSLayoutConstraint! = grabberHandle.topAnchor.constraint(equalTo: topAnchor, constant: grabberTopPadding) + private lazy var grabberHandleWidthConstraint: NSLayoutConstraint = grabberHandle.widthAnchor.constraint(equalToConstant: grabberHandleWidth) + private lazy var grabberHandleHeightConstraint: NSLayoutConstraint = grabberHandle.heightAnchor.constraint(equalToConstant: grabberHandleHeight) + private lazy var grabberHandleTopConstraint: NSLayoutConstraint = grabberHandle.topAnchor.constraint(equalTo: topAnchor, constant: grabberTopPadding) override init(frame: CGRect) { super.init(frame: frame) @@ -135,7 +152,12 @@ public class FloatingPanelSurfaceView: UIView { super.updateConstraints() containerViewTopInsetConstraint.constant = containerTopInset containerViewHeightConstraint.constant = bottomOverflow + + contentViewTopConstraint?.constant = contentInsets.top + contentViewLeftConstraint?.constant = contentInsets.left + contentViewRightConstraint?.constant = contentInsets.right contentViewHeightConstraint?.constant = -containerTopInset + grabberHandleTopConstraint.constant = grabberTopPadding grabberHandleWidthConstraint.constant = grabberHandleWidth grabberHandleHeightConstraint.constant = grabberHandleHeight @@ -194,13 +216,20 @@ public class FloatingPanelSurfaceView: UIView { self.contentView = contentView /* contentView.frame = bounds */ // MUST NOT: Because the top safe area inset of a content VC will be incorrect. contentView.translatesAutoresizingMaskIntoConstraints = false - let contentViewHeightConstraint = contentView.heightAnchor.constraint(equalTo: heightAnchor, constant: -containerTopInset) + + let topConstraint = contentView.topAnchor.constraint(equalTo: topAnchor, constant: contentInsets.top) + let leftConstraint = contentView.leftAnchor.constraint(equalTo: leftAnchor, constant: contentInsets.left) + let rightConstraint = rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: contentInsets.right) + let heightConstraint = contentView.heightAnchor.constraint(equalTo: heightAnchor, constant: -containerTopInset) NSLayoutConstraint.activate([ - contentView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0.0), - contentView.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 0.0), - contentView.rightAnchor.constraint(equalTo: containerView.rightAnchor, constant: 0.0), - contentViewHeightConstraint, + topConstraint, + leftConstraint, + rightConstraint, + heightConstraint, ]) - self.contentViewHeightConstraint = contentViewHeightConstraint + self.contentViewTopConstraint = topConstraint + self.contentViewLeftConstraint = leftConstraint + self.contentViewRightConstraint = rightConstraint + self.contentViewHeightConstraint = heightConstraint } }