我正在用 swift 编写一个基本的自定义转换。我的问题是,出于某种原因,过渡正在用圆角插入呈现 View Controller 。为什么会发生这种情况?是否可以禁用它?
呈现 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 中,.automatic
是 UIViewController
的默认 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/