SwiftUI - in sheet have a fixed continue button that is not scrollable

前端 未结 2 889
别跟我提以往
别跟我提以往 2020-12-10 23:21

As you can see even though I am trying to pull the sheet down, the continue button does not move down. How can I make my sheet to behave like that? In my app the con

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 23:48

    Simply add that :

    .sheet(isPresented: self.$visibleSheet) {
        IntroView(visibleSheet: self.$visibleSheet)
            .presentation(shouldDismissOnDrag: false)
    }
    

    https://stackoverflow.com/a/61239704/7974174 :

    extension View {
       func presentation(shouldDismissOnDrag: Bool, onDismissalAttempt: (()->())? = nil) -> some View {
           ModalView(view: self, shouldDismiss: shouldDismissOnDrag, onDismissalAttempt: onDismissalAttempt)
       }
    }
    
    struct ModalView: UIViewControllerRepresentable {
       let view: T
       let shouldDismiss: Bool
       let onDismissalAttempt: (()->())?
       
       func makeUIViewController(context: Context) -> UIHostingController {
           UIHostingController(rootView: view)
       }
       
       func updateUIViewController(_ uiViewController: UIHostingController, context: Context) {
           uiViewController.parent?.presentationController?.delegate = context.coordinator
       }
       
       func makeCoordinator() -> Coordinator {
           Coordinator(self)
       }
       
       class Coordinator: NSObject, UIAdaptivePresentationControllerDelegate {
           let modalView: ModalView
           
           init(_ modalView: ModalView) {
               self.modalView = modalView
           }
           
           func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
               modalView.shouldDismiss
           }
           
           func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
               modalView.onDismissalAttempt?()
           }
       }
    }
    
    

    It disables the sheet closing by dragging the sheet down. If you want to close the sheet with the button do not use presentationMode anymore. Pass a binding of self.$visibleSheet then modify to false from inside...

提交回复
热议问题