Skip to content

Commit

Permalink
Added ContentInset Property on SurfaceView API (#200)
Browse files Browse the repository at this point in the history
* Added Show ContentInset to Example application
  • Loading branch information
SvenTiigi authored and scenee committed Jun 1, 2019
1 parent a9a6543 commit cf70929
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
15 changes: 15 additions & 0 deletions Examples/Samples/Sources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SampleListViewController: UIViewController {
case showNestedScrollView
case showRemovablePanel
case showIntrinsicView
case showContentInset

var name: String {
switch self {
Expand All @@ -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"
}
}

Expand All @@ -51,6 +53,7 @@ class SampleListViewController: UIViewController {
case .showNestedScrollView: return "NestedScrollViewController"
case .showRemovablePanel: return "DetailViewController"
case .showIntrinsicView: return "IntrinsicViewController"
case .showContentInset: return nil
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down
53 changes: 41 additions & 12 deletions Framework/Sources/FloatingPanelSurfaceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

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)

/// 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 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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit cf70929

Please sign in to comment.