2016-10-11 73 views
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) 
     } 
    ) 
} 
+2

只是將'let containerView = transitionContext.containerView'移出guard語句並將其放在'containerView.insertSubview(toVC.view,belowSubview:fromVC.view)之上' –

回答

0

這是因爲containerView屬性不是一個可選類型,你可以在這裏看到(這是從文檔(命令+ LMB)採取UIViewControllerContextTransitioing):

... 
public protocol UIViewControllerContextTransitioning : NSObjectProtocol { 


    // The view in which the animated transition should take place. 

    @available(iOS 2.0, *) 
    public var containerView: UIView { get } 
... 

您可以在guard聲明之後放置錯誤的行。

相關問題