Slide 15
Slide 15 text
©︎
2021 Yahoo Japan Corporation All rights reserved.
iOS 13༻ʹϑϧεΫϦʔϯϞʔμϧ࣮Ͱ͖Δ
struct ContentView: View {
@State var isPresented = false
var body: some View {
Button("Hello, world") {
isPresented = true
}
.myFullScreenCover(isPresented: $isPresented) {
NextView()
}
}
}
struct NextView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button("Dismiss") {
presentationMode.wrappedValue.dismiss()
}
}
}
extension View {
@ViewBuilder func myFullScreenCover(
isPresented: Binding, @ViewBuilder content: @escaping () -> Content) -> some View {
if #available(iOS 14, *) {
fullScreenCover(isPresented: isPresented, content: content)
} else {
FullScreenCover(isPresented: isPresented, content: self, destination: content)
}
}
}
struct FullScreenCover: View {
var isPresented: Binding
let content: Content
let destination: () -> Destination
@State var viewController: UIViewController? = nil
var body: some View {
content
.lookUp($viewController)
.onReceive(Just(isPresented.wrappedValue)) { isPresented in
if isPresented {
let controller = UIHostingController(rootView: destination())
controller.modalPresentationStyle = .fullScreen
viewController?.present(controller, animated: true, completion: nil)
} else {
viewController?.dismiss(animated: true, completion: nil)
}
}
}
}
Ͱ͖Δ͚ͩUIKitײΛग़ͣ͞ɺࠓޙϐϡΞͳSwiftUIʹҠߦ͍͢͠I/Fʹ͢Δͱྑ͍