【发布时间】:2016-10-11 03:55:24
【问题描述】:
我最近下载了这个项目并将其转换为最新的 swift 语法。我不明白为什么我继续收到错误“条件绑定的初始化程序必须具有可选类型,而不是“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移出保护语句并将其放在containerView.insertSubview(toVC.view, belowSubview: fromVC.view)上方
标签: swift3