1
我最近下載了該項目並將其轉換爲最新的swift語法。我不明白爲什麼我繼續得到錯誤「初始值設定條件結合必須有可選的類型,而不是‘UIView的’條件綁定的初始化程序必須具有可選類型,而不是「UIView」
誤差線存在的:
let containerView = transitionContext.containerView
以下是完整的代碼:
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard
let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to),
let containerView = transitionContext.containerView
else {
return
}
containerView.insertSubview(toVC.view, belowSubview: fromVC.view)
let screenBounds = UIScreen.main.bounds
let bottomLeftCorner = CGPoint(x: 0, y: screenBounds.height)
let finalFrame = CGRect(origin: bottomLeftCorner, size: screenBounds.size)
UIView.animate(
withDuration: transitionDuration(using: transitionContext),
animations: {
fromVC.view.frame = finalFrame
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
}
只是將'let containerView = transitionContext.containerView'移出guard語句並將其放在'containerView.insertSubview(toVC.view,belowSubview:fromVC.view)之上' –