1 Star 0 Fork 0

greyCode / PartialSheet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

PartialSheet

A custom SwiftUI modifier to present a Partial Modal Sheet based on his content size.

Version 2.0 has been released, there are a lot of breaking changes, make sure to read the guide before update!

Index

iPhone Preview

iPad Preview

Mac Preview

Features

Availables

  • Slidable and dismissable with drag gesture
  • Variable height based on his content
  • Customizable colors
  • Keyboard compatibility
  • Landscape compatibility
  • iOS compatibility
  • iPad compatibility
  • Mac compatibility

Nice to have

  • ScrollView and List compatibility: as soon as Apple adds some API to handle better ScrollViews

Version 2

The new version brings a lot of breaking changes and a lot of improvments:

  • The Partial Sheet can now be called from any view in the navigation stack
  • The Partial Sheet can now be called from any item inside a List
  • The Partial Sheet is now handled as an environment object making easy to display and close it.

Installation

Requirements

  • iOS 13.0+ / macOS 10.15+
  • Xcode 11.2+
  • Swift 5+

Via Swift Package Manager

In Xcode 11 or grater, in you project, select: File > Swift Packages > Add Pacakage Dependency.

In the search bar type PartialSheet and when you find the package, with the next button you can proceed with the installation.

If you can't find anything in the panel of the Swift Packages you probably haven't added yet your github account. You can do that under the Preferences panel of your Xcode, in the Accounts section.

How to Use

You can read more on the wiki - full guide.

To use the Partial Sheet you need to follow just three simple steps

  1. Add a Partial Sheet Manager instance as an environment object to your Root View in you SceneDelegate
// 1.1 Create the manager
let sheetManager: PartialSheetManager = PartialSheetManager()
let contentView = ContentView()
    // 1.2 Add the manager as environmentObject
    .environmentObject(sheetManager)

//Common SwiftUI code to add the rootView in your rootViewController
if let windowScene = scene as? UIWindowScene {
    let window = UIWindow(windowScene: windowScene)
    window.rootViewController = UIHostingController(
        rootView: contentView
    )
    self.window = window
    window.makeKeyAndVisible()
}
  1. Add the Partial View to your Root View, and if you want give it a style. In your RootView file at the end of the builder add the following modifier:
struct ContentView: View {

    var body: some View {
       ...
       .addPartialSheet(style: <PartialSheetStyle>)
    }
}
  1. In anyone of your views add a reference to the environment object and than just call the showPartialSheet<T>(_ onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> T) where T: View func whenever you want like this:
@EnvironmentObject var partialSheetManager: PartialSheetManager

...

Button(action: {
    self.partialSheetManager.showPartialSheet({
        print("Partial sheet dismissed")
    }) {
         Text("This is a Partial Sheet")
    }
}, label: {
    Text("Show sheet")
})

You can also show the Partial Sheet using a view modifier:

@State var isSheetShown = false

...

Button(action: {
    self.isSheetShown = true
}, label: {
    Text("Display the ViewModifier sheet")
})
.partialSheet(isPresented: $isSheetShown) {
    Text("This is a Partial Sheet")
}

If you want a starting point copy in your SceneDelegate and in your ContentView files the following code:

  1. SceneDelegate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    let sheetManager: PartialSheetManager = PartialSheetManager()
    let contentView = ContentView()
        .environmentObject(sheetManager)
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(
            rootView: contentView
        )
        self.window = window
        window.makeKeyAndVisible()
    }
}
  1. ContentView:
import SwiftUI
import PartialSheet

struct ContentView: View {

    @EnvironmentObject var partialSheet : PartialSheetManager

    var body: some View {
        NavigationView {
            VStack(alignment: .center) {
                Spacer()
                    Button(action: {
                        self.partialSheet.showPartialSheet({
                            print("dismissed")
                        }) {
                            Text("Partial Sheet")
                        }
                    }, label: {
                        Text("Show Partial Sheet")
                    })
                Spacer()
            }
            .navigationBarTitle("Partial Sheet")
        }
        .navigationViewStyle(StackNavigationViewStyle())
        .addPartialSheet()
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Remember to always add import PartialSheet in every file you want to use the PartialSheet.

In the Example directory you can find more examples with more complex structures.

Using Pickers

When using pickers it is needed to register an onTapGesture. This some how makes the picker being able to reconize the drag before the dragGesture on the sheet.

struct PickerSheetView: View {
    var strengths = ["Mild", "Medium", "Mature"]
    @State private var selectedStrength = 0

    var body: some View {
        VStack {
            VStack {
                Text("Settings Panel").font(.headline)
                Picker(selection: $selectedStrength, label: EmptyView()) {
                    ForEach(0 ..< strengths.count) {
                        Text(self.strengths[$0])
                    }
                }.onTapGesture {
                    // Fixes issue with scroll
                }
            }
            .padding()
            .frame(height: 250)
        }
    }
}
MIT License Copyright (c) 2020 Andrea Miotto Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
Swift
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Swift
1
https://gitee.com/greycode/PartialSheet.git
git@gitee.com:greycode/PartialSheet.git
greycode
PartialSheet
PartialSheet
master

搜索帮助