首页 > 解决方案 > iOS Share to Mail 应用程序始终在顶部显示文本,在底部显示图像,而不是跟随数组中的内容

问题描述

我正在尝试将内容从我的应用程序共享到邮件。特别是我想拥有:

然而,似乎我所有的文字都被推到了顶部,而图像在底部结束。我做错了什么,我该如何完成这个订单?

我目前正在分享

let content: [Any] = ["Top text", UIImage(systemName: "pencil")!, "Bottom Text"]

并得到以下结果,这不是我想要的。

在此处输入图像描述

这是我正在使用的代码。

import SwiftUI

enum Coordinator {
    static func topViewController(_ viewController: UIViewController? = nil) -> UIViewController? {
        let vc = viewController ?? UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.rootViewController
        if let navigationController = vc as? UINavigationController {
            return topViewController(navigationController.topViewController)
        } else if let tabBarController = vc as? UITabBarController {
            return tabBarController.presentedViewController != nil ? topViewController(tabBarController.presentedViewController) : topViewController(tabBarController.selectedViewController)
            
        } else if let presentedViewController = vc?.presentedViewController {
            return topViewController(presentedViewController)
        }
        return vc
    }
}

struct ContentView: View {
    
    func share() {
        let content: [Any] = ["Top text", UIImage(systemName: "pencil")!, "Bottom Text"]
        
        let activityViewController = UIActivityViewController(activityItems: content, applicationActivities: nil)
        let viewController = Coordinator.topViewController()
        activityViewController.popoverPresentationController?.sourceView = viewController?.view
        activityViewController.completionWithItemsHandler = {(activityType: UIActivity.ActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
        }
        
        viewController?.present(activityViewController, animated: true, completion: nil)
    }
    
    var body: some View {
        Button(action: share) {
            Text("Share")
        }
        .padding()
    }
}

标签: swiftswiftui

解决方案


推荐阅读