2017-09-04 31 views
0

我想從我創建一個自定義動畫,其正常工作頂部添加視圖控制器。然而,這是所添加的視圖控制器的背景顏色設置爲黑色,在那裏,因爲我只是希望它是透明的,使得下面的視圖控制器可以是可見的。斯威夫特 - 讓UIViewController中可見,同時增加動畫從TOP(通過self.present)新的ViewController

這裏是添加自定義動畫代碼:

let vc = Global.sharedInstance.storyboard.instantiateViewController(withIdentifier: "AnimationViewControllerID") as! AnimationViewController 
      vc.view.backgroundColor = UIColor.clear 
     vc.personName = name 
     let begin = CGRect(x: 0, y: -vc.view.frame.height, width: vc.view.frame.width, height: vc.view.frame.height) 
     let end = CGRect(x: 0, y: 0, width: vc.view.frame.width, height: vc.view.frame.width) 
     self.present(vc, animated: false) {() -> Void in 
      vc.view.frame = begin 
      vc.view.alpha = 0.5 
      vc.view.backgroundColor = UIColor.clear 
      UIView.animate(withDuration: 1, animations: {() -> Void in 
       vc.view.frame = end 
      }, 
          completion: nil) 
     } 

我嘗試設置視圖控制器的背景顏色來清除,但仍,它不是使背景透明。

+1

這種技術(無動畫呈現,然後動畫演示​​完成後)是有限的(你不能讓它互動;外觀的方法'viewDidAppear'被稱爲在錯誤的時間,等等) 。你想要的是自定義轉換。見https://stackoverflow.com/a/42213998/1271826或https://stackoverflow.com/a/45764323/1271826自定義轉換的例子通過'present'。 – Rob

+0

@Rob先生感謝您的信息。 –

回答

2

應設置呈現視圖控制器的表現風格相應。

如果您希望將其顯示在當前窗口上,則將其設置爲overFullScreen;如果您只是想將其設置爲高於視圖控制器的上下文,則將其設置爲overCurrentContext

此外,你應該設置definesPresentationContexttrue

vc.modalPresentationStyle = .overFullScreen 
vc.definesPresentationContext = true 
+1

這是正確的,但.overCurrentContext也在這裏工作不是嗎? –

+0

@MikeAlter在某些情況下,是的。編輯我的答案。 – the4kman