众所周知,常规UIViewController 的iOS 13+模态表示样式将默认为 .pageSheet 。如果要更改它,可以在显示时将其更改为所需的样式。我在我的应用程序中使用 MFMailComposeViewController 和 MFMessageComposeViewController 共享内容。在 MFMailComposeViewController 的情况下,当我选择 modalPresentationStyle = .fullScreen 时,它可以很好地工作... 但不是 MFMessageComposeViewController 的情况。请在下面找到代码段
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")
}
}
答案 0 :(得分:0)
虽然我无法回答关于*为什么不能对MFMessageComposeViewController使用.fullScreen的问题,但我可以告诉您,如果您实现自己的自定义转换,则会得到所需的行为。
这是您为视图控制器设置自定义过渡的方式:
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)
})
}
}