Skip to content

Commit

Permalink
Implement by content anchor mode
Browse files Browse the repository at this point in the history
  • Loading branch information
havebeenfitz committed May 10, 2021
1 parent 4e7e7df commit e3bdc1f
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 41 deletions.
2 changes: 1 addition & 1 deletion AnchoredBottomSheet.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'AnchoredBottomSheet'
s.version = '1.2.4'
s.version = '1.3.0'
s.summary = 'iOS Maps like bottom sheet with configurable anchors and reusable pannable View'

s.description = <<-DESC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ExampleTableViewDataSource: NSObject {

extension ExampleTableViewDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
return 5
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
Expand Down
40 changes: 14 additions & 26 deletions Example/AnchoredBottomSheet/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ViewController: UIViewController {
fileprivate func presentModal(at index: Int) {
switch index {
case 0:
presentModalWithStackView()
presentWithContentHeight()
case 1:
presentModalWithTableView()
case 2:
Expand All @@ -54,29 +54,11 @@ class ViewController: UIViewController {
}
}

private func presentModalWithStackView() {
let button1 = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 60))
button1.backgroundColor = .green

let button2 = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 60))
button2.backgroundColor = .red

let button3 = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 60))
button3.backgroundColor = .yellow


let stackView = UIStackView()
stackView.axis = .vertical
stackView.spacing = 5
stackView.distribution = .fillEqually
stackView.addArrangedSubview(button1)
stackView.addArrangedSubview(button2)
stackView.addArrangedSubview(button3)

let configuration = BottomSheetViewConfiguration(contentView: stackView,
private func presentWithContentHeight() {
let configuration = BottomSheetViewConfiguration(contentView: data.tableView,
parentViewController: self,
defaultPosition: .middle(),
positions: [.middle(), .top()],
defaultPosition: .byContent,
positions: [.byContent],
isPullIndicatorNeeded: true,
closeButtonIcon: UIImage(named: "icon16Close"),
isDismissAllowed: true)
Expand Down Expand Up @@ -112,26 +94,32 @@ class ViewController: UIViewController {

extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
presentModal(at: indexPath.row)
}
}

extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BasicCell", for: indexPath)
cell.textLabel?.numberOfLines = 0
switch indexPath.row {
case 0:
cell.textLabel?.text = "Present with stackView"
cell.textLabel?.text = "Present tableView with it's content height"
return cell
case 1:
cell.textLabel?.text = "Present with tableView"
cell.textLabel?.text = "Present tableView with middle,\ntop and bottom anchors"
return cell
case 2:
cell.textLabel?.text = "Push \"split screen\""
cell.textLabel?.text = "Push \"split screen\" with\naccessible background"
return cell
default:
return UITableViewCell()
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- AnchoredBottomSheet (1.2.0)
- AnchoredBottomSheet (1.3.0)

DEPENDENCIES:
- AnchoredBottomSheet (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
AnchoredBottomSheet: 67b3c0f44ed4e508c0bf66308c2c48f3c13973df
AnchoredBottomSheet: 61c3b5e7b7b448f24899ba2e1fcba6762cbc0b94

PODFILE CHECKSUM: 9d2fa57cad45113c77064b354f9582a45331dfbf

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/AnchoredBottomSheet.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

⚠️ SnapKit is no longer a dependency since 1.2.0

## About
🎉 ByContent anchor mode is available since 1.3.0. BottomSheetView resizes itself to match the subviews content height as long as it's smaller than screen height

<img height="500" src="https://user-images.githubusercontent.com/31866271/117650439-5dfe6d80-b199-11eb-89d5-e8df63268827.gif"><img height="500" src="https://user-images.githubusercontent.com/31866271/117653913-b20b5100-b19d-11eb-9ff7-613d11c3caa2.gif">
## About

![Demo](https://user-images.githubusercontent.com/31866271/117659897-2ac1db80-b1a5-11eb-903e-bdd562fd8c38.gif)

This is iOS Maps like anchored bottom sheet with configurable anchors and reusable `BottomSheetView`, that can be used inside provided modal controller or without it.

Expand All @@ -40,6 +41,7 @@ Supported anchors:
2. Middle
3. Bottom
4. Fixed
5. By content

Each anchor has associated value, which will override default height for this anchor.
By content mode is not yet implemented, feel free to open a pull request.
Expand Down
17 changes: 13 additions & 4 deletions Sources/AnchoredBottomSheet/BottomSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public class BottomSheetView: UIView {

private var closeButton: UIButton?

private var maxSheetHeight: CGFloat {
UIScreen.main.bounds.height - UIApplication.shared.statusBarFrame.height
}

public init(configuration: BottomSheetViewConfiguration) {
self.contentView = configuration.contentView
self.parentViewController = configuration.parentViewController
Expand Down Expand Up @@ -98,12 +102,17 @@ public class BottomSheetView: UIView {
case .byContent:
if let scrollView = contentView as? UIScrollView {
let finalRect = scrollView.contentSize
byContentPositionHeight = finalRect.height + 70
byContentPositionHeight = min(finalRect.height + 70, maxSheetHeight)
} else {
let finalRect: CGRect = contentView.subviews.reduce(into: .zero) { rect, subview in
rect = rect.union(subview.frame)
if let scrollView = contentView as? UIScrollView {
let finalRect = scrollView.contentSize
byContentPositionHeight = min(finalRect.height + 70, maxSheetHeight)
} else {
let finalRect: CGRect = contentView.subviews.reduce(into: .zero) { rect, subview in
rect = rect.union(subview.frame)
}
byContentPositionHeight = finalRect.height
}
byContentPositionHeight = finalRect.height
}
}
}
Expand Down

0 comments on commit e3bdc1f

Please sign in to comment.