Skip to content
Permalink
Browse files

Add a background to the bottom sheet that is updated when scrolling.

  • Loading branch information...
skagedal committed Jul 31, 2018
1 parent a2ec162 commit 1d7037de627bb58452d06d9ca04e64aabffcaa98
@@ -12,6 +12,7 @@
F4EAE159210FBD4100487323 /* MapViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE158210FBD4100487323 /* MapViewController.swift */; };
F4EAE15B210FBD4C00487323 /* CountriesTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE15A210FBD4C00487323 /* CountriesTableViewController.swift */; };
F4EAE15D210FBD5B00487323 /* BottomSheetContainerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE15C210FBD5B00487323 /* BottomSheetContainerViewController.swift */; };
F4EAE15F2110884E00487323 /* BottomSheetBackgroundView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE15E2110884E00487323 /* BottomSheetBackgroundView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
@@ -22,6 +23,7 @@
F4EAE158210FBD4100487323 /* MapViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MapViewController.swift; sourceTree = "<group>"; };
F4EAE15A210FBD4C00487323 /* CountriesTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CountriesTableViewController.swift; sourceTree = "<group>"; };
F4EAE15C210FBD5B00487323 /* BottomSheetContainerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BottomSheetContainerViewController.swift; sourceTree = "<group>"; };
F4EAE15E2110884E00487323 /* BottomSheetBackgroundView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BottomSheetBackgroundView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
@@ -58,6 +60,7 @@
F4EAE14F210FBD0100487323 /* LaunchScreen.storyboard */,
F4EAE146210FBCFE00487323 /* AppDelegate.swift */,
F4EAE15C210FBD5B00487323 /* BottomSheetContainerViewController.swift */,
F4EAE15E2110884E00487323 /* BottomSheetBackgroundView.swift */,
F4EAE158210FBD4100487323 /* MapViewController.swift */,
F4EAE15A210FBD4C00487323 /* CountriesTableViewController.swift */,
);
@@ -136,6 +139,7 @@
F4EAE15B210FBD4C00487323 /* CountriesTableViewController.swift in Sources */,
F4EAE147210FBCFE00487323 /* AppDelegate.swift in Sources */,
F4EAE159210FBD4100487323 /* MapViewController.swift in Sources */,
F4EAE15F2110884E00487323 /* BottomSheetBackgroundView.swift in Sources */,
F4EAE15D210FBD5B00487323 /* BottomSheetContainerViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -0,0 +1,19 @@
//
// Copyright © 2018 Simon Kågedal Reimer. All rights reserved.
//
import UIKit

class BottomSheetBackgroundView: UIView {

override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = .white
}

required init?(coder aDecoder: NSCoder) {
fatalError()
}

}
@@ -4,10 +4,22 @@
import UIKit

protocol BottomSheetDelegate: AnyObject {
func bottomSheet(_ bottomSheet: BottomSheet, didScrollTo contentOffset: CGPoint)
}

protocol BottomSheet: AnyObject {
var bottomSheetDelegate: BottomSheetDelegate? { get set }
}

typealias BottomSheetViewController = UIViewController & BottomSheet

class BottomSheetContainerView: UIView {

private let mainView: UIView
private let sheetView: UIView
private let sheetBackground = BottomSheetBackgroundView()
private var sheetBackgroundTopConstraint: NSLayoutConstraint? = nil

init(mainView: UIView, sheetView: UIView) {
self.mainView = mainView
@@ -20,6 +32,12 @@ class BottomSheetContainerView: UIView {

required init?(coder aDecoder: NSCoder) { fatalError() }

var topDistance: CGFloat = 0 {
didSet {
sheetBackgroundTopConstraint?.constant = topDistance
}
}

private func setupViews() {
// The main view fills the view completely
addSubview(mainView)
@@ -31,6 +49,18 @@ class BottomSheetContainerView: UIView {
mainView.bottomAnchor.constraint(equalTo: bottomAnchor)
])

// The sheet background
addSubview(sheetBackground)
sheetBackground.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = sheetBackground.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor)
NSLayoutConstraint.activate([
topConstraint,
sheetBackground.heightAnchor.constraint(equalTo: heightAnchor),
sheetBackground.leftAnchor.constraint(equalTo: leftAnchor),
sheetBackground.rightAnchor.constraint(equalTo: rightAnchor)
])
sheetBackgroundTopConstraint = topConstraint

// The sheet table view goes all the way up to the status bar
addSubview(sheetView)
sheetView.translatesAutoresizingMaskIntoConstraints = false
@@ -47,18 +77,20 @@ class BottomSheetContainerView: UIView {
class BottomSheetContainerViewController: UIViewController {

private let mainViewController: UIViewController
private let sheetViewController: UIViewController
private let sheetViewController: BottomSheetViewController
private lazy var bottomSheetContainerView = BottomSheetContainerView(mainView: mainViewController.view,
sheetView: sheetViewController.view)

init(mainViewController: UIViewController, sheetViewController: UIViewController) {
init(mainViewController: UIViewController, sheetViewController: BottomSheetViewController) {
self.mainViewController = mainViewController
self.sheetViewController = sheetViewController

super.init(nibName: nil, bundle: nil)

addChild(mainViewController)
addChild(sheetViewController)

sheetViewController.bottomSheetDelegate = self
}

required init?(coder aDecoder: NSCoder) {
@@ -75,5 +107,11 @@ class BottomSheetContainerViewController: UIViewController {
mainViewController.didMove(toParent: self)
sheetViewController.didMove(toParent: self)
}

}

extension BottomSheetContainerViewController: BottomSheetDelegate {
func bottomSheet(_ bottomSheet: BottomSheet, didScrollTo contentOffset: CGPoint) {
bottomSheetContainerView.topDistance = max(0, -contentOffset.y)
}
}
@@ -10,8 +10,10 @@ private let numberOfCountries = 20
private let countries = Locale.isoRegionCodes.prefix(numberOfCountries).map(Locale.current.localizedString(forRegionCode:))
private let reuseIdentifier = "cell"

class CountriesTableViewController: UITableViewController {

class CountriesTableViewController: UITableViewController, BottomSheet {

var bottomSheetDelegate: BottomSheetDelegate?

override func viewDidLoad() {
super.viewDidLoad()

@@ -22,6 +24,12 @@ class CountriesTableViewController: UITableViewController {
tableView.showsVerticalScrollIndicator = false
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

bottomSheetDelegate?.bottomSheet(self, didScrollTo: tableView.contentOffset)
}

// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

0 comments on commit 1d7037d

Please sign in to comment.
You can’t perform that action at this time.