iOS 13 modalPresentationStyle = UIModalPresentationFullScreen 不适用于 MFMessageComposeViewController

Posted

技术标签:

【中文标题】iOS 13 modalPresentationStyle = UIModalPresentationFullScreen 不适用于 MFMessageComposeViewController【英文标题】:iOS 13 modalPresentationStyle = UIModalPresentationFullScreen not working for MFMessageComposeViewController 【发布时间】:2019-11-25 11:51:37 【问题描述】:

ios 13+ 的模态显示样式中我们都知道,普通 UIViewController 将默认为 .pageSheet。如果您想更改它,您可以在展示时将其更改为您想要的样式。我在我的应用程序中使用 MFMailComposeViewControllerMFMessageComposeViewController 来共享内容。在 MFMailComposeViewController 的情况下,当我选择 modalPresentationStyle = .fullScreen 时,它工作得非常好...... 但对于 MFMessageComposeViewController 则不然。请在下面找到代码sn-p

        if (MFMessageComposeViewController.canSendText()) 
            let controller = MFMessageComposeViewController()
            controller.body = “Message Body”
            controller.messageComposeDelegate = self
            controller.modalPresentationStyle = .fullScreen
            self.present(controller, animated: true, completion: nil)
            self.trackEvent(shareType: "SMS")
        
    

【问题讨论】:

我的回答对你没有帮助? @Alex.Pinhasov 这很有帮助,但我不是在寻找自定义实现,我只是在检查我是否以错误的方式进行操作。但它似乎与MFMailComposeViewController 本身有问题。 @iGauravK 你能告诉我,你解决了这个问题吗?以及如何? @UsamaAzam 由于模态演示样式在这种情况下不起作用,而且我不想要任何自定义转换,所以答案是否定的。 【参考方案1】:

虽然我无法回答您关于 *为什么不能将 .fullScreen 用于 MFMessageComposeViewController 的问题,但我可以告诉您,如果您实现自己的自定义转换,您将获得所需的行为。

这是您为视图控制器设置自定义转换的方式:

var customTransitionDelegate = CustomTransition(presentAnimation: CustomActionSheetPresentationTransition(), dismissAnimation: CustomActionSheetDismissalTransition())

 if (MFMessageComposeViewController.canSendText()) 
     let controller = MFMessageComposeViewController()
     controller.body = "Message Body"
     controller.messageComposeDelegate = self
     controller.modalPresentationStyle = .custom
     controller.transitioningDelegate = customTransitionDelegate
     self.present(controller, animated: true, completion: nil)
 

您还应该遵守委托并检查结果以在用户单击取消后将其关闭:

    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) 
    switch result 
    case .cancelled:
        self.presentedViewController?.dismiss(animated: true, completion: nil)
    default: break
    

表示逻辑

class CustomActionSheetPresentationTransition: NSObject, UIViewControllerAnimatedTransitioning 
private let duration = 0.6

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 
    guard let fromView = transitionContext.viewController(forKey: .from) else  return 
    guard let toView   = transitionContext.viewController(forKey: .to) else  return 

    var screenOffUp = CGAffineTransform()
    let container = transitionContext.containerView

    screenOffUp = CGAffineTransform(translationX: 0, y: -fromView.view.frame.height)
    toView.view.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: fromView.view.frame.width, height: fromView.view.frame.height)

    toView.view.center.x = container.center.x
    container.addSubview(toView.view)

    UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: 
        toView.view.transform = screenOffUp
    , completion:  (success) in
        transitionContext.completeTransition(success)
    )


func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval 
    return duration


解雇逻辑

class CustomActionSheetDismissalTransition: NSObject, UIViewControllerAnimatedTransitioning 
private let duration = 0.8

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval 
    return duration


func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 
    guard let fromView = transitionContext.viewController(forKey: .from) else  return 
    let screenOffDown = CGAffineTransform(translationX: 0, y: fromView.view.frame.height)

    UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.8, options: [], animations: 
        fromView.view.transform = screenOffDown
    , completion:  (success) in
        transitionContext.completeTransition(success)
    )

【讨论】:

以上是关于iOS 13 modalPresentationStyle = UIModalPresentationFullScreen 不适用于 MFMessageComposeViewController的主要内容,如果未能解决你的问题,请参考以下文章

苹果13有必要升级ios16吗?

iOS 13设置 tabbar字体

iOS13.6越狱有望/iOS13.6以及iOS12.4.8推送更新/苹果加入新人权Emoji表情

iOS 13.6 降级 13.4.1 系统,FilzaEscaped 进展

iOS13系统UITabBar

iOS 13.5.1iOS 12.4.7 验证关闭,用户无法刷机