如何在 SwiftUI 中更改 MFMailComposeViewController 导航栏的外观?

问题描述

我正在尝试更改 MFMailComposeViewController 导航栏的外观。 UINavigationBar 外观被全局设置(在 AppDelegate 的 didFinishLaunchingWithOptions 函数)为所需的颜色,但是,当 MFMailComposeViewController 打开时,导航栏会稍微延迟恢复为认值。

我的尝试(基于之前对此类问题的一些回答):

  • makeUIViewController 函数中设置 viewController 的导航栏
  • 在初始化 UINavigationBar.appearance() 之前调用外观方法 (MFMailComposeViewController)
  • 覆盖 MFMailComposeViewControllerviewDidLoad 方法

这是一个错误吗?如果不是,我该如何更改 MFMailComposeViewController 的导航栏的外观?

如何重现:(Xcode 版本:12.3,iOS 版本:14.3)

MailViewNavbarapp.swift

class AppDelegate: NSObject,UIApplicationDelegate {
    func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        let colorednavAppearance = UINavigationBarappearance()
        colorednavAppearance.configureWithOpaqueBackground()
        colorednavAppearance.backgroundColor = .gray
        colorednavAppearance.titleTextAttributes = [.foregroundColor: UIColor.yellow]
        colorednavAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.yellow]

        UINavigationBar.appearance().tintColor = .yellow
        UINavigationBar.appearance().standardAppearance = colorednavAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = colorednavAppearance
        return true
    }
}


@main
struct MailViewNavbarapp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

MailView.swift

struct MailView: UIViewControllerRepresentable {
    @Binding var isShowing: Bool
    let email: String

    func makeCoordinator() -> Coordinator {
        Coordinator(isShowing: $isShowing)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<MailView>) -> MFMailComposeViewController {
        let vc = MFMailComposeViewController()
        vc.setToRecipients([email])
        vc.mailComposeDelegate = context.coordinator

        return vc
    }

    func updateUIViewController(
        _ vc: MFMailComposeViewController,context _: UIViewControllerRepresentableContext<MailView>
    ) {}

    class Coordinator: NSObject,MFMailComposeViewControllerDelegate {
        @Binding var isShowing: Bool

        init(isShowing: Binding<Bool>) {
            _isShowing = isShowing
        }

        func mailComposeController(_: MFMailComposeViewController,didFinishWith _: MFMailComposeResult,error: Error?) {
            isShowing = false
        }
    }
}

ContentView.swift

import SwiftUI
import MessageUI

struct ContentView: View {
    @State var isShowingMailView = false
    
    var body: some View {
        Button(action: {
            self.isShowingMailView.toggle()
        }) {
            Text("Tap me")
        }
        .disabled(!MFMailComposeViewController.canSendMail())
        .sheet(isPresented: $isShowingMailView) {
            MailView(
                isShowing: $isShowingMailView,email: "test@email.com"
            )
        }
    }
}

感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)