首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >支持滑动即可关闭工作表中显示的UIViewControllerRepresentable

支持滑动即可关闭工作表中显示的UIViewControllerRepresentable
EN

Stack Overflow用户
提问于 2020-07-25 01:43:45
回答 1查看 316关注 0票数 4

似乎如果你使用UIViewControllerRepresentable在你的SwiftUI应用程序中实现一个视图控制器,当你通过sheet呈现它时,你不能滑动它来关闭它。你需要做些什么来支持swipe解除吗?

代码语言:javascript
复制
struct ContentView: View {
    @State var showingPicker = false
    
    var body: some View {
        Text("Hello, world!")
            .onAppear {
                showingPicker = true
            }
            .sheet(isPresented: $showingPicker, content: {
                PHPicker() //cannot swipe to dismiss
                //Text("Test") //can swipe to dismiss
            })
    }
}

struct PHPicker: UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<PHPicker>) -> PHPickerViewController {
        let config = PHPickerConfiguration()
        return PHPickerViewController(configuration: config)
    }
    
    func updateUIViewController(_ uiViewController: PHPickerViewController, context: UIViewControllerRepresentableContext<PHPicker>) { }
}
EN

回答 1

Stack Overflow用户

发布于 2021-01-25 22:25:49

可能的解决方案是添加类似句柄的东西来拖动(没有样式-简化了演示),

代码语言:javascript
复制
.sheet(isPresented: $showingPicker, content: {
        VStack {
            RoundedRectangle(cornerRadius: 8).fill(Color.gray)
                .frame(width: 60, height: 8)
                .padding(.top, 8)
            PHPicker()
        }
})

替代:解决方案是完全由UIKit表示,只需在内部传递激活绑定即可表示。

这是一个可能的方法的演示。使用Xcode12.1/ iOS 14.1进行测试

代码语言:javascript
复制
struct PHPickerContentView: View {
    @State var showingPicker = false
    
    var body: some View {
        Button("Picker") {
           showingPicker = true
        }
        .background(PHPicker(isPresented: $showingPicker))    // << here !!
    }
}

struct PHPicker: UIViewControllerRepresentable {
    @Binding var isPresented: Bool

    func makeUIViewController(context: Context) -> UIViewController {
        UIViewController()   // << picker presenter
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        // react on binding & show if not shown
        if isPresented && uiViewController.presentedViewController == nil {
            let config = PHPickerConfiguration()
            let picker = PHPickerViewController(configuration: config)
            picker.delegate = context.coordinator

            uiViewController.present(picker, animated: true)
            picker.presentationController?.delegate = context.coordinator
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, PHPickerViewControllerDelegate, UIAdaptivePresentationControllerDelegate {
        let owner: PHPicker
        init(_ owner: PHPicker) {
            self.owner = owner
        }

        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {

            // picked image handling code here
        
            picker.presentingViewController?.dismiss(animated: true)
            owner.isPresented = false    // << reset on action !!
        }
        
        func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
            owner.isPresented = false    // << reset on swipe !!
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63078836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档