MFMessageComposeViewController + SwiftUI 错误行为

问题描述

我正在使用 ViewControllerRepresentable 来呈现 MFMessageComposeViewController,以便用户可以从我的应用发送文本。

然而,每次呈现视图时,它都非常有问题 - 元素随机消失,滚动关闭,屏幕闪烁。在 iOS 14.2 和 14.3 上测试。

代码如下:

import SwiftUI
import MessageUI

struct MessageView: UIViewControllerRepresentable {
    var recipient: String
    
    class Coordinator: NSObject,MFMessageComposeViewControllerDelegate {
        var completion: () -> Void
        init(completion: @escaping ()->Void) {
            self.completion = completion
        }
        
        // delegate method
        func messageComposeViewController(_ controller: MFMessageComposeViewController,didFinishWith result: MessageComposeResult) {
            controller.dismiss(animated: true,completion: nil)
            completion()
        }
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator() {} // not using completion handler
    }
    
    func makeUIViewController(context: Context) -> MFMessageComposeViewController {
        let vc = MFMessageComposeViewController()
        vc.recipients = [recipient]

        vc.messageComposeDelegate = context.coordinator
        return vc
    }
    
    func updateUIViewController(_ uiViewController: MFMessageComposeViewController,context: Context) {}
    
    typealias UIViewControllerType = MFMessageComposeViewController
}

我的观点

struct ContentView: View {
    @State private var isShowingMessages = false
    @State var result: Result<MFMailComposeResult,Error>? = nil
    var body: some View {
        vstack {
            Button("Show Messages") {
                self.isShowingMessages = true
            }
            .sheet(isPresented: self.$isShowingMessages) {
            MessageView(recipient: "+15555555555")
        }
            .edgesIgnoringSafeArea(.bottom)
        }
    }
}

我呈现这种观点的方式有什么问题吗?有没有其他人经历过这种行为? MFMailComposeViewController 也会发生类似的行为,但没有那么严重。

解决方法

5 分钟后,我意识到我需要在展示工作表时添​​加此内容:

this.columnDefinitions: Column[] = [
   // ...
   { id: 'valueDate',field: 'valueDate',name: 'Transaction Date ',formatter: Formatters.dateEuro },];

this.gridOptions: GridOption = {
    // you can customize the date separator through "formatterOptions"
    formatterOptions: {
      // What separator to use to display a Date,for example using "." it could be "2002.01.01"
      dateSeparator: '-',// can be any of '/' | '-' | '.' | ' ' | ''
    }
};

该视图看起来有问题,因为它试图考虑键盘安全区域并且很难做到。