Skip to content

Commit

Permalink
Add BottomSheet in view extension
Browse files Browse the repository at this point in the history
  • Loading branch information
weitieda committed Apr 30, 2020
1 parent ee3d569 commit 4ded9c5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
7 changes: 3 additions & 4 deletions Sources/BottomSheet/BottomSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ public struct BottomSheet<Content: View>: View {
@State private var previousDragValue: DragGesture.Value?

@Binding var isPresented: Bool
let maxHeight: CGFloat
private let maxHeight: CGFloat
private let content: Content

let contentBackgroundColor: Color
let topBarBackgroundColor: Color
private let contentBackgroundColor: Color
private let topBarBackgroundColor: Color

public init(
isPresented: Binding<Bool>,
Expand Down
4 changes: 2 additions & 2 deletions Sources/BottomSheet/RoundedCorner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ extension View {
}
}

public struct RoundedCorner: Shape {
struct RoundedCorner: Shape {
var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners

public func path(in rect: CGRect) -> Path {
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
return Path(path.cgPath)
}
Expand Down
29 changes: 29 additions & 0 deletions Sources/BottomSheet/ViewExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ViewExtension.swift
// BottomSheet
//
// Created by Tieda Wei on 2020-04-25.
// Copyright © 2020 Tieda Wei. All rights reserved.
//

import SwiftUI

public extension View {
func bottomSheet<Content: View>(
isPresented: Binding<Bool>,
maxHeight: CGFloat,
contentBackgroundColor: Color = Color(.systemBackground),
topBarBackgroundColor: Color = Color(.systemBackground),
@ViewBuilder content: @escaping () -> Content
) -> some View {
ZStack {
self
BottomSheet(isPresented: isPresented,
maxHeight: maxHeight,
topBarBackgroundColor: topBarBackgroundColor,
contentBackgroundColor: contentBackgroundColor,
content: content)
}
}
}

22 changes: 9 additions & 13 deletions iOS Example/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@ struct ContentView: View {
@State var isPresented = true

var body: some View {
ZStack {
NavigationView {
ZStack {
Color.secondary
Text("Hi")
}.navigationBarItems(trailing:
Button(action: { self.isPresented = true }) {
Text("Show")
}
).navigationBarTitle("Bottom Sheet")
NavigationView {
List(0..<20) {
Text("\($0)")
}
BottomSheet(isPresented: $isPresented, maxHeight: 500) {
// Text("Hello")
List(0..<20) { Text("\($0)") }
.bottomSheet(isPresented: self.$isPresented, maxHeight: 300) {
List(20..<40) { Text("\($0)") }
}
.navigationBarTitle("Bottom Sheet")
.navigationBarItems(
trailing: Button(action: { self.isPresented = true }) { Text("Show") }
)
}
}
}
Expand Down

0 comments on commit 4ded9c5

Please sign in to comment.