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

时间:2021-01-04 17:21:48

标签: ios swift swiftui uinavigationbar 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"
            )
        }
    }
}

感谢您的帮助!

0 个答案:

没有答案