Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed crash #78

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Example/UBottomSheet/Cells/EmbeddedCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ class EmbeddedCell: UITableViewCell, UICollectionViewDataSource, UICollectionVie
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HorizontalCell", for: indexPath) as! HorizontalCell
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HorizontalCell", for: indexPath) as? HorizontalCell else {
return UICollectionViewCell()
}
cell.configure(with: items[indexPath.item])
return cell
}

}
4 changes: 2 additions & 2 deletions Example/UBottomSheet/DataSource/MyDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import UBottomSheet

class MyDataSource: UBottomSheetCoordinatorDataSource {
func sheetPositions(_ availableHeight: CGFloat) -> [CGFloat] {
return [0.2, 0.4, 0.6, 0.8].map{$0*availableHeight}
return [0.2, 0.4, 0.6, 0.8].map{ $0 * availableHeight }
}

func initialPosition(_ availableHeight: CGFloat) -> CGFloat {
return availableHeight*0.4
return availableHeight * 0.4
}
}
4 changes: 2 additions & 2 deletions Example/UBottomSheet/DataSource/PullToDismissDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import UBottomSheet

class PullToDismissDataSource: UBottomSheetCoordinatorDataSource {
func sheetPositions(_ availableHeight: CGFloat) -> [CGFloat] {
return [0.5, 1.1].map{$0*availableHeight} /// Trick is to set bottom position to any value more than available height such as 1.1*availableHeight
return [0.5, 1.1].map{ $0 * availableHeight } /// Trick is to set bottom position to any value more than available height such as 1.1*availableHeight
}

func initialPosition(_ availableHeight: CGFloat) -> CGFloat {
return availableHeight*0.5
return availableHeight * 0.5
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ extension AppleMapsSheetViewController: UITableViewDelegate, UITableViewDataSour
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "EmbeddedCell", for: indexPath) as! EmbeddedCell
guard let cell = tableView.dequeueReusableCell(withIdentifier: "EmbeddedCell", for: indexPath) as? EmbeddedCell else {
return UITableViewCell()
}
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "MapItemCell", for: indexPath) as! MapItemCell
let cell = tableView.dequeueReusableCell(withIdentifier: "MapItemCell", for: indexPath) as? MapItemCell else {
return UITableViewCell()
}
let title: String
let subtitle: String
switch indexPath.row {
Expand All @@ -102,9 +106,10 @@ extension AppleMapsSheetViewController: UITableViewDelegate, UITableViewDataSour

switch indexPath.row {
case 0:
let sc = UBottomSheetCoordinator(parent: sheetCoordinator!.parent)
guard let sheetCoordinator = sheetCoordinator else { return }
let sc = UBottomSheetCoordinator(parent: sheetCoordinator.parent)
vc.sheetCoordinator = sc
sc.addSheet(vc, to: sheetCoordinator!.parent)
sc.addSheet(vc, to: sheetCoordinator.parent)
case 1:
vc.sheetCoordinator = sheetCoordinator
sheetCoordinator?.addSheetChild(vc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ extension ListViewController: UITableViewDelegate, UITableViewDataSource{
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MapItemCell", for: indexPath) as! MapItemCell
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MapItemCell", for: indexPath) as? MapItemCell else {
return UITableViewCell()
}
let title: String
let subtitle: String
switch indexPath.row {
Expand All @@ -76,9 +78,10 @@ extension ListViewController: UITableViewDelegate, UITableViewDataSource{
let vc = LabelViewController()
switch indexPath.row {
case 0:
let sc = UBottomSheetCoordinator(parent: sheetCoordinator!.parent)
guard let sheetCoordinator = sheetCoordinator else { return }
let sc = UBottomSheetCoordinator(parent: sheetCoordinator.parent)
vc.sheetCoordinator = sc
sc.addSheet(vc, to: sheetCoordinator!.parent)
sc.addSheet(vc, to: sheetCoordinator.parent)
case 1:
vc.sheetCoordinator = sheetCoordinator
sheetCoordinator?.addSheetChild(vc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import UBottomSheet

class MapsViewController: UIViewController {
var sheetCoordinator: UBottomSheetCoordinator!
var sheetCoordinator: UBottomSheetCoordinator?
var backView: PassThroughView?

override func viewDidLoad() {
Expand All @@ -36,21 +36,21 @@ class MapsViewController: UIViewController {

}

private func addBackDimmingBackView(below container: UIView){
backView = PassThroughView()
self.view.insertSubview(backView!, belowSubview: container)
backView!.translatesAutoresizingMaskIntoConstraints = false
backView!.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
backView!.bottomAnchor.constraint(equalTo: container.topAnchor, constant: 10).isActive = true
backView!.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
backView!.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
private func addBackDimmingBackView(below container: UIView) {
guard let backView = PassThroughView() else { return }
self.view.insertSubview(backView, belowSubview: container)
backView.translatesAutoresizingMaskIntoConstraints = false
backView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
backView.bottomAnchor.constraint(equalTo: container.topAnchor, constant: 10).isActive = true
backView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
backView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
}
}

extension MapsViewController: UBottomSheetCoordinatorDelegate{
extension MapsViewController: UBottomSheetCoordinatorDelegate {

func bottomSheet(_ container: UIView?, didPresent state: SheetTranslationState) {
// self.addBackDimmingBackView(below: container!)
// self.addBackDimmingBackView(below: container!) // if later uncommented, remove the force unwrap and add a guard let for the container
self.sheetCoordinator.addDropShadowIfNotExist()
self.handleState(state)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import UIKit
import UBottomSheet

class SimpleViewController: UIViewController {
var sheetCoordinator: UBottomSheetCoordinator!
var sheetCoordinator: UBottomSheetCoordinator?

var sheetVC: DraggableItem!
var sheetVC: DraggableItem?
var useNavController = false
var dataSource: UBottomSheetCoordinatorDataSource?

Expand All @@ -25,10 +25,10 @@ class SimpleViewController: UIViewController {

guard sheetCoordinator == nil else {return}
sheetCoordinator = UBottomSheetCoordinator(parent: self)
if dataSource != nil{
sheetCoordinator.dataSource = dataSource!
if let dataSource = dataSource {
sheetCoordinator.dataSource = dataSource
}

let vc: UIViewController
if useNavController{
vc = UINavigationController(rootViewController: sheetVC)
Expand Down
5 changes: 3 additions & 2 deletions Sources/UBottomSheet/Classes/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ extension UIView {
}

extension Array where Element == CGFloat {
func nearest(to x: CGFloat) -> CGFloat {
return self.reduce(self.first!) { abs($1 - x) < abs($0 - x) ? $1 : $0 }
func nearest(to x: CGFloat) -> CGFloat? {
guard let first = first else { return nil }
return self.reduce(first) { abs($1 - x) < abs($0 - x) ? $1 : $0 }
}
}
Loading