swift - 使用自定义过渡时,为什么 UIDropShadowView 会插入呈现 View Controller ?

标签 swift xcode

我正在用 swift 编写一个基本的自定义转换。我的问题是,出于某种原因,过渡正在用圆角插入呈现 View Controller 。为什么会发生这种情况?是否可以禁用它?

custom_transition

呈现 View Controller :

final class ViewController: UIViewController {

    let card = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(card)
        card.heightAnchor == 200
        card.horizontalAnchors == view.horizontalAnchors + 24
        card.centerAnchors == view.centerAnchors

        card.backgroundColor = .black
        card.layer.cornerRadius = 8

        view.backgroundColor = .white

        card.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(presentView)))

    }

    @objc func presentView() {

        let detail = DetailViewController()
        detail.transitioningDelegate = self
        present(detail, animated: true, completion: nil)

    }
}

extension ViewController: UIViewControllerTransitioningDelegate {
    public func animationController(forPresented presented: UIViewController,
                                    presenting: UIViewController,
                                    source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CardModalPresentAnimator()
    }
}

呈现的 View Controller :

final class DetailViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
    }
}

自定义转换:

final class CardModalPresentAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.4
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        guard
            let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as? ViewController,
            let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? DetailViewController
            else { return }

        let containerView = transitionContext.containerView
        containerView.insertSubview(toVC.view, aboveSubview: fromVC.view)
        toVC.view.frame = fromVC.card.frame

        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            // center the modal on the screen
            toVC.view.frame = UIScreen.main.bounds
        }, completion: { _ in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        })
    }
}

最佳答案

在 iOS 13 中,.automaticUIViewController 的默认 modalPresentationStyle。对于标准模式呈现,请使用 .fullScreen

public enum UIModalPresentationStyle : Int {


    case fullScreen

    @available(iOS 3.2, *)
    case pageSheet

    @available(iOS 3.2, *)
    case formSheet

    @available(iOS 3.2, *)
    case currentContext

    @available(iOS 7.0, *)
    case custom

    @available(iOS 8.0, *)
    case overFullScreen

    @available(iOS 8.0, *)
    case overCurrentContext

    @available(iOS 8.0, *)
    case popover


    @available(iOS 7.0, *)
    case none

    @available(iOS 13.0, *)
    case automatic
}

关于swift - 使用自定义过渡时,为什么 UIDropShadowView 会插入呈现 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57299390/

相关文章:

json - 快速 JSON 解析

ios 约束不起作用

objective-c - AppDelegate、rootViewController 和 presentViewController

objective-c - Objective-C : Triggering IBAction showInfo in viewDidAppear

swift - Realm Swift使用Object作为参数

arrays - Swift - 通过重复生成组合

swift - Dart 中的列表排序与 Swift 中的排序列表不同

xcode - 如何使用苹果推送通知?

ios - 上传高分 - 如何让它只对 friend 可见?

ios - Swift:class does not implement methodSignatureForSelector: -- 前面有麻烦无法识别的选择器