Skip to content

Commit 55f124b

Browse files
committed
Replace FloatingPanelController.show(_:sender:) with the set(contentViewController:)
And add a sample code to test show(_:sender:) in ContentVC. `FloatingPanelController.show(_:sender:)` can block 'Show' segue in a content view controller. 'Show' segue should not be handled by 'FloatingPanelController`. So I replace this method.
1 parent 580c708 commit 55f124b

File tree

6 files changed

+144
-40
lines changed

6 files changed

+144
-40
lines changed

Examples/Maps/Maps/ViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class ViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate,
2626

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

29-
// Add a content view controller
30-
fpc.show(searchVC, sender: self)
29+
// Set a content view controller
30+
fpc.set(contentViewController: searchVC)
3131
fpc.track(scrollView: searchVC.tableView)
3232

3333
setupMapView()

Examples/Samples/Sources/Base.lproj/Main.storyboard

+30-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="RoN-h0-uBD">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="RoN-h0-uBD">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14283.14"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
99
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
@@ -356,7 +356,7 @@
356356
</connections>
357357
</swipeGestureRecognizer>
358358
</objects>
359-
<point key="canvasLocation" x="1239" y="806"/>
359+
<point key="canvasLocation" x="1311" y="806"/>
360360
</scene>
361361
<!--Detail View Controller-->
362362
<scene sceneID="b6k-zi-3wn">
@@ -390,14 +390,36 @@
390390
<constraint firstAttribute="height" constant="44" id="DQJ-cY-cKx"/>
391391
</constraints>
392392
</view>
393+
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="22" translatesAutoresizingMaskIntoConstraints="NO" id="tP3-oJ-4EB">
394+
<rect key="frame" x="130.5" y="108" width="114" height="82"/>
395+
<subviews>
396+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="c5r-jU-haj">
397+
<rect key="frame" x="0.0" y="0.0" width="114" height="30"/>
398+
<state key="normal" title="Show"/>
399+
<connections>
400+
<action selector="buttonPressed:" destination="YC8-ae-15L" eventType="touchUpInside" id="Mi1-o6-TWt"/>
401+
</connections>
402+
</button>
403+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="wmd-ab-Nz3">
404+
<rect key="frame" x="0.0" y="52" width="114" height="30"/>
405+
<state key="normal" title="Present Modallly"/>
406+
<connections>
407+
<action selector="buttonPressed:" destination="YC8-ae-15L" eventType="touchUpInside" id="tjH-Ev-kpx"/>
408+
<segue destination="bYI-y3-Rzb" kind="presentation" identifier="PresentModallySegue" id="3yq-HE-Tgn"/>
409+
</connections>
410+
</button>
411+
</subviews>
412+
</stackView>
393413
</subviews>
394414
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
395415
<gestureRecognizers/>
396416
<constraints>
397417
<constraint firstItem="noi-1a-5bZ" firstAttribute="top" secondItem="g7l-kO-y7q" secondAttribute="top" constant="12" id="EQy-cr-F2Y"/>
418+
<constraint firstItem="tP3-oJ-4EB" firstAttribute="centerX" secondItem="g7l-kO-y7q" secondAttribute="centerX" id="EsD-Vf-dNZ"/>
398419
<constraint firstItem="8yw-OC-Ubk" firstAttribute="bottom" secondItem="g7l-kO-y7q" secondAttribute="bottom" id="JOL-wC-w74"/>
399420
<constraint firstItem="8yw-OC-Ubk" firstAttribute="leading" secondItem="tAi-nk-rDB" secondAttribute="leading" id="RiJ-Hb-OOZ"/>
400421
<constraint firstItem="8yw-OC-Ubk" firstAttribute="trailing" secondItem="tAi-nk-rDB" secondAttribute="trailing" id="Sof-yL-mwK"/>
422+
<constraint firstItem="tP3-oJ-4EB" firstAttribute="top" secondItem="tAi-nk-rDB" secondAttribute="top" constant="88" id="Zhb-Ss-epe"/>
401423
<constraint firstItem="Kva-Z7-0qY" firstAttribute="trailing" secondItem="tAi-nk-rDB" secondAttribute="trailing" id="kkp-Yo-FQW"/>
402424
<constraint firstItem="tAi-nk-rDB" firstAttribute="trailing" secondItem="noi-1a-5bZ" secondAttribute="trailing" constant="12" id="lv9-Nf-HNB"/>
403425
<constraint firstItem="Kva-Z7-0qY" firstAttribute="leading" secondItem="tAi-nk-rDB" secondAttribute="leading" id="oVC-i1-TwS"/>
@@ -413,6 +435,7 @@
413435
<size key="freeformSize" width="375" height="778"/>
414436
<connections>
415437
<outlet property="closeButton" destination="noi-1a-5bZ" id="eWQ-ha-8y7"/>
438+
<segue destination="bYI-y3-Rzb" kind="show" identifier="ShowSegue" id="r1P-2i-NDe"/>
416439
</connections>
417440
</viewController>
418441
<placeholder placeholderIdentifier="IBFirstResponder" id="Wqk-xl-O3I" userLabel="First Responder" sceneMemberID="firstResponder"/>
@@ -432,7 +455,7 @@
432455
</connections>
433456
</pongPressGestureRecognizer>
434457
</objects>
435-
<point key="canvasLocation" x="1442" y="-23"/>
458+
<point key="canvasLocation" x="1440.8" y="-23.388305847076463"/>
436459
</scene>
437460
<!--Debug Text View Controller-->
438461
<scene sceneID="Bkq-O7-q4A">
@@ -507,4 +530,7 @@ Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
507530
<point key="canvasLocation" x="729" y="-23"/>
508531
</scene>
509532
</scenes>
533+
<inferredMetricsTieBreakers>
534+
<segue reference="3yq-HE-Tgn"/>
535+
</inferredMetricsTieBreakers>
510536
</document>

Examples/Samples/Sources/ViewController.swift

+24-12
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ class SampleListViewController: UIViewController, UITableViewDataSource, UITable
6464
mainPanelVC.surfaceView.cornerRadius = 6.0
6565
mainPanelVC.surfaceView.shadowHidden = false
6666

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

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

123-
// Add a content view controller and connect with the scroll view
124-
detailPanelVC.show(contentVC, sender: self)
125-
126-
// (contentVC as? DetailViewController)?.closeButton?.addTarget(self, action: #selector(dismissDetailPanelVC), for: .touchUpInside)
124+
// Set a content view controller
125+
detailPanelVC.set(contentViewController: contentVC)
127126

128127
// Add FloatingPanel to self.view
129128
detailPanelVC.addPanel(toParent: self, belowView: nil, animated: true)
@@ -260,6 +259,18 @@ class DetailViewController: UIViewController {
260259
// dismiss(animated: true, completion: nil)
261260
(self.parent as? FloatingPanelController)?.removePanelFromParent(animated: true, completion: nil)
262261
}
262+
263+
@IBAction func buttonPressed(_ sender: UIButton) {
264+
switch sender.titleLabel?.text {
265+
case "Show":
266+
performSegue(withIdentifier: "ShowSegue", sender: self)
267+
case "Present Modally":
268+
performSegue(withIdentifier: "PresentModallySegue", sender: self)
269+
default:
270+
break
271+
}
272+
}
273+
263274
@IBAction func tapped(_ sender: Any) {
264275
print("Detail panel is tapped!")
265276
}
@@ -284,12 +295,13 @@ class ModalViewController: UIViewController {
284295
fpc.surfaceView.cornerRadius = 6.0
285296
fpc.surfaceView.shadowHidden = false
286297

287-
// Add a content view controller and connect with the scroll view
298+
// Set a content view controller and track the scroll view
288299
let consoleVC = storyboard?.instantiateViewController(withIdentifier: "ConsoleViewController") as! DebugTextViewController
289-
fpc.show(consoleVC, sender: self)
290-
self.consoleVC = consoleVC
300+
fpc.set(contentViewController: consoleVC)
291301
fpc.track(scrollView: consoleVC.textView)
292302

303+
self.consoleVC = consoleVC
304+
293305
// Add FloatingPanel to self.view
294306
fpc.addPanel(toParent: self, belowView: safeAreaView)
295307
}
@@ -331,11 +343,11 @@ class TabBarContentViewController: UIViewController, FloatingPanelControllerDele
331343
fpc.surfaceView.cornerRadius = 6.0
332344
fpc.surfaceView.shadowHidden = false
333345

334-
// Add a content view controller and connect with the scroll view
346+
// Set a content view controller and track the scroll view
335347
let consoleVC = storyboard?.instantiateViewController(withIdentifier: "ConsoleViewController") as! DebugTextViewController
336-
fpc.show(consoleVC, sender: self)
337-
self.consoleVC = consoleVC
348+
fpc.set(contentViewController: consoleVC)
338349
fpc.track(scrollView: consoleVC.textView)
350+
self.consoleVC = consoleVC
339351

340352
// Add FloatingPanel to self.view
341353
fpc.addPanel(toParent: self)

Examples/Stocks/Stocks/ViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class ViewController: UIViewController, FloatingPanelControllerDelegate {
3434

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

37-
// Add a content view controller
38-
fpc.show(newsVC, sender: self)
37+
// Set a content view controller
38+
fpc.set(contentViewController: newsVC)
3939
fpc.track(scrollView: newsVC.scrollView)
4040

4141
fpc.addPanel(toParent: self, belowView: bottomToolView, animated: false)

Framework/Sources/FloatingPanelController.swift

+46-14
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI
9191
/// 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.
9292
public var contentInsetAdjustmentBehavior: ContentInsetAdjustmentBehavior = .always
9393

94+
private var _contentViewController: UIViewController?
95+
public var contentViewController: UIViewController? {
96+
set { set(contentViewController: newValue) }
97+
get { return _contentViewController }
98+
}
99+
94100
private var floatingPanel: FloatingPanel!
95101

96102
required init?(coder aDecoder: NSCoder) {
@@ -102,12 +108,14 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI
102108
}
103109

104110
/// Initialize a newly created floating panel controller.
105-
public init() {
111+
public init(contentViewController: UIViewController? = nil) {
106112
super.init(nibName: nil, bundle: nil)
107113

108114
floatingPanel = FloatingPanel(self,
109115
layout: fetchLayout(for: self.traitCollection),
110116
behavior: fetchBehavior(for: self.traitCollection))
117+
118+
set(contentViewController: contentViewController)
111119
}
112120

113121
/// Creates the view that the controller manages.
@@ -243,20 +251,44 @@ public class FloatingPanelController: UIViewController, UIScrollViewDelegate, UI
243251
floatingPanel.move(to: to, animated: animated, completion: completion)
244252
}
245253

246-
/// Presents the specified view controller as the content view controller in the surface view interface.
254+
/// Sets the view controller responsible for the content portion of the floating panel..
255+
public func set(contentViewController: UIViewController?) {
256+
if let vc = _contentViewController {
257+
vc.willMove(toParent: nil)
258+
vc.view.removeFromSuperview()
259+
vc.removeFromParent()
260+
}
261+
262+
if let vc = contentViewController {
263+
let surfaceView = self.view as! FloatingPanelSurfaceView
264+
surfaceView.contentView.addSubview(vc.view)
265+
vc.view.frame = surfaceView.contentView.bounds
266+
vc.view.translatesAutoresizingMaskIntoConstraints = false
267+
NSLayoutConstraint.activate([
268+
vc.view.topAnchor.constraint(equalTo: surfaceView.contentView.topAnchor, constant: 0.0),
269+
vc.view.leftAnchor.constraint(equalTo: surfaceView.contentView.leftAnchor, constant: 0.0),
270+
vc.view.rightAnchor.constraint(equalTo: surfaceView.contentView.rightAnchor, constant: 0.0),
271+
vc.view.bottomAnchor.constraint(equalTo: surfaceView.contentView.bottomAnchor, constant: 0.0),
272+
])
273+
addChild(vc)
274+
vc.didMove(toParent: self)
275+
}
276+
277+
_contentViewController = contentViewController
278+
}
279+
280+
@available(*, unavailable, renamed: "set(contentViewController:)")
247281
public override func show(_ vc: UIViewController, sender: Any?) {
248-
let surfaceView = self.view as! FloatingPanelSurfaceView
249-
surfaceView.contentView.addSubview(vc.view)
250-
vc.view.frame = surfaceView.contentView.bounds
251-
vc.view.translatesAutoresizingMaskIntoConstraints = false
252-
NSLayoutConstraint.activate([
253-
vc.view.topAnchor.constraint(equalTo: surfaceView.contentView.topAnchor, constant: 0.0),
254-
vc.view.leftAnchor.constraint(equalTo: surfaceView.contentView.leftAnchor, constant: 0.0),
255-
vc.view.rightAnchor.constraint(equalTo: surfaceView.contentView.rightAnchor, constant: 0.0),
256-
vc.view.bottomAnchor.constraint(equalTo: surfaceView.contentView.bottomAnchor, constant: 0.0),
257-
])
258-
addChild(vc)
259-
vc.didMove(toParent: self)
282+
if let target = self.parent?.targetViewController(forAction: #selector(UIViewController.show(_:sender:)), sender: sender) {
283+
target.show(vc, sender: sender)
284+
}
285+
}
286+
287+
@available(*, unavailable, renamed: "set(contentViewController:)")
288+
public override func showDetailViewController(_ vc: UIViewController, sender: Any?) {
289+
if let target = self.parent?.targetViewController(forAction: #selector(UIViewController.showDetailViewController(_:sender:)), sender: sender) {
290+
target.showDetailViewController(vc, sender: sender)
291+
}
260292
}
261293

262294
// MARK: - Scroll view tracking

0 commit comments

Comments
 (0)