Skip to content

Commit

Permalink
Add FloatingPanelController.set(contentViewController:)
Browse files Browse the repository at this point in the history
  • Loading branch information
scenee committed Oct 31, 2018
1 parent 2031678 commit 514c3c9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Examples/Maps/Maps/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate,

searchVC = storyboard?.instantiateViewController(withIdentifier: "SearchPanel") as? SearchPanelViewController

// Add a content view controller
fpc.show(searchVC, sender: self)
// Set a content view controller
fpc.set(contentViewController: searchVC)
fpc.track(scrollView: searchVC.tableView)

setupMapView()
Expand Down
25 changes: 13 additions & 12 deletions Examples/Samples/Sources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ class SampleListViewController: UIViewController, UITableViewDataSource, UITable
mainPanelVC.surfaceView.cornerRadius = 6.0
mainPanelVC.surfaceView.shadowHidden = false

// Add a content view controller and connect with the scroll view
mainPanelVC.show(contentVC, sender: self)
// Set a content view controller
mainPanelVC.set(contentViewController: contentVC)

// Track a scroll view
switch contentVC {
case let consoleVC as DebugTextViewController:
mainPanelVC.track(scrollView: consoleVC.textView)
Expand Down Expand Up @@ -120,10 +121,8 @@ class SampleListViewController: UIViewController, UITableViewDataSource, UITable
detailPanelVC.surfaceView.cornerRadius = 6.0
detailPanelVC.surfaceView.shadowHidden = false

// Add a content view controller and connect with the scroll view
detailPanelVC.show(contentVC, sender: self)

// (contentVC as? DetailViewController)?.closeButton?.addTarget(self, action: #selector(dismissDetailPanelVC), for: .touchUpInside)
// Set a content view controller
detailPanelVC.set(contentViewController: contentVC)

// Add FloatingPanel to self.view
detailPanelVC.addPanel(toParent: self, belowView: nil, animated: true)
Expand Down Expand Up @@ -262,6 +261,7 @@ class DetailViewController: UIViewController {
}

@IBAction func buttonPressed(_ sender: Any) {
// A fatal error will occurrs
performSegue(withIdentifier: "ShowSegue", sender: self)
}

Expand Down Expand Up @@ -289,12 +289,13 @@ class ModalViewController: UIViewController {
fpc.surfaceView.cornerRadius = 6.0
fpc.surfaceView.shadowHidden = false

// Add a content view controller and connect with the scroll view
// Set a content view controller and track the scroll view
let consoleVC = storyboard?.instantiateViewController(withIdentifier: "ConsoleViewController") as! DebugTextViewController
fpc.show(consoleVC, sender: self)
self.consoleVC = consoleVC
fpc.set(contentViewController: consoleVC)
fpc.track(scrollView: consoleVC.textView)

self.consoleVC = consoleVC

// Add FloatingPanel to self.view
fpc.addPanel(toParent: self, belowView: safeAreaView)
}
Expand Down Expand Up @@ -336,11 +337,11 @@ class TabBarContentViewController: UIViewController, FloatingPanelControllerDele
fpc.surfaceView.cornerRadius = 6.0
fpc.surfaceView.shadowHidden = false

// Add a content view controller and connect with the scroll view
// Set a content view controller and track the scroll view
let consoleVC = storyboard?.instantiateViewController(withIdentifier: "ConsoleViewController") as! DebugTextViewController
fpc.show(consoleVC, sender: self)
self.consoleVC = consoleVC
fpc.set(contentViewController: consoleVC)
fpc.track(scrollView: consoleVC.textView)
self.consoleVC = consoleVC

// Add FloatingPanel to self.view
fpc.addPanel(toParent: self)
Expand Down
4 changes: 2 additions & 2 deletions Examples/Stocks/Stocks/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class ViewController: UIViewController, FloatingPanelControllerDelegate {

newsVC = storyboard?.instantiateViewController(withIdentifier: "News") as? NewsViewController

// Add a content view controller
fpc.show(newsVC, sender: self)
// Set a content view controller
fpc.set(contentViewController: newsVC)
fpc.track(scrollView: newsVC.scrollView)

fpc.addPanel(toParent: self, belowView: bottomToolView, animated: false)
Expand Down
63 changes: 37 additions & 26 deletions Framework/Sources/FloatingPanelController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI
/// This property specifies how the content area of the tracking scroll view is modified using `adjustedContentInsets`. The default value of this property is FloatingPanelController.ContentInsetAdjustmentBehavior.always.
public var contentInsetAdjustmentBehavior: ContentInsetAdjustmentBehavior = .always

private var _contentViewController: UIViewController?
public var contentViewController: UIViewController? {
set { set(contentViewController: newValue) }
get { return _contentViewController }
}

private var floatingPanel: FloatingPanel!

required init?(coder aDecoder: NSCoder) {
Expand All @@ -102,7 +108,7 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI
}

/// Initialize a newly created floating panel controller.
public init() {
public init(contentViewController: UIViewController? = nil) {
super.init(nibName: nil, bundle: nil)

floatingPanel = FloatingPanel(self,
Expand Down Expand Up @@ -241,35 +247,40 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI
floatingPanel.move(to: to, animated: animated, completion: completion)
}

/// Presents the specified view controller as the content view controller in the surface view interface.
///
/// - Attention:
/// You can't use this method to replace the content view controller.
public override func show(_ vc: UIViewController, sender: Any?) {
if self.children.first != nil, let ancester = self.parent?.targetViewController(forAction: #selector(show(_:sender:)), sender: sender) {
ancester.show(vc, sender: sender)
return
/// Sets the view controller responsible for the content portion of the floating panel..
public func set(contentViewController: UIViewController?) {
if let vc = _contentViewController {
vc.willMove(toParent: nil)
vc.view.removeFromSuperview()
vc.removeFromParent()
}
show(vc)

if let vc = contentViewController {
let surfaceView = self.view as! FloatingPanelSurfaceView
surfaceView.contentView.addSubview(vc.view)
vc.view.frame = surfaceView.contentView.bounds
vc.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
vc.view.topAnchor.constraint(equalTo: surfaceView.contentView.topAnchor, constant: 0.0),
vc.view.leftAnchor.constraint(equalTo: surfaceView.contentView.leftAnchor, constant: 0.0),
vc.view.rightAnchor.constraint(equalTo: surfaceView.contentView.rightAnchor, constant: 0.0),
vc.view.bottomAnchor.constraint(equalTo: surfaceView.contentView.bottomAnchor, constant: 0.0),
])
addChild(vc)
vc.didMove(toParent: self)
}

_contentViewController = contentViewController
}

public override func showDetailViewController(_ vc: UIViewController, sender: Any?) {
show(vc)
@available(*, unavailable, renamed: "set(contentViewController:)")
public override func show(_ vc: UIViewController, sender: Any?) {
fatalError("Not allowed to use 'Show' kind segue from the controller. Please add another floating panel to show a detail or add a navigation controller in a content view controller")
}

private func show(_ vc: UIViewController) {
let surfaceView = self.view as! FloatingPanelSurfaceView
surfaceView.contentView.addSubview(vc.view)
vc.view.frame = surfaceView.contentView.bounds
vc.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
vc.view.topAnchor.constraint(equalTo: surfaceView.contentView.topAnchor, constant: 0.0),
vc.view.leftAnchor.constraint(equalTo: surfaceView.contentView.leftAnchor, constant: 0.0),
vc.view.rightAnchor.constraint(equalTo: surfaceView.contentView.rightAnchor, constant: 0.0),
vc.view.bottomAnchor.constraint(equalTo: surfaceView.contentView.bottomAnchor, constant: 0.0),
])
addChild(vc)
vc.didMove(toParent: self)

@available(*, unavailable, message: "set(contentViewController:)")
public override func showDetailViewController(_ vc: UIViewController, sender: Any?) {
fatalError("Not allowed to use 'Show Detail' kind segue from the controller. Please add another floating panel to show a detail or add a navigation controller in a content view controller")
}

// MARK: - Scroll view tracking
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ class ViewController: UIViewController, FloatingPanelControllerDelegate {
// Assign self as the delegate of the controller.
fpc.delegate = self // Optional

// Add a content view controller.
// Set a content view controller.
let contentVC = ContentViewController()
fpc.show(contentVC, sender: nil)
fpc.set(viewController: contentVC)

// Track a scroll view(or the siblings) in the content view controller.
fpc.track(scrollView: contentVC.tableView)

// Add the views managed by the `FloatingPanelController` object to self.view.
// Add and show the views managed by the `FloatingPanelController` object to self.view.
fpc.addPanel(toParent: self)
}

Expand Down Expand Up @@ -222,7 +222,7 @@ class ViewController: UIViewController, FloatingPanelControllerDelegate {
self.searchPanelVC = FloatingPanelController()

let searchVC = SearchViewController()
self.searchPanelVC.show(searchVC, sender: nil)
self.searchPanelVC.set(viewController: searchVC)
self.searchPanelVC.track(scrollView: contentVC.tableView)

self.searchPanelVC.addPanel(toParent: self)
Expand All @@ -231,7 +231,7 @@ class ViewController: UIViewController, FloatingPanelControllerDelegate {
self.detailPanelVC = FloatingPanelController()

let contentVC = ContentViewController()
self.detailPanelVC.show(contentVC, sender: nil)
self.searchPanelVC.set(viewController: contentVC)
self.detailPanelVC.track(scrollView: contentVC.scrollView)

self.detailPanelVC.addPanel(toParent: self)
Expand Down

0 comments on commit 514c3c9

Please sign in to comment.