2017-02-23 38 views
1

我在使用UIBlurEffectUIViewController中使用了.crossDissolve過渡樣式。 UIViewController在其視圖中添加了一個collectionView,所以它們都有清晰的背景。UINavigationController - 使用UIBlurEffect清除背景

HomeViewController.swift

func showLiveDealsCategory(sender:UITextField){ 
    let catSelection = LiveDealCategorySelection() 
    //let navContr = UINavigationController(rootViewController: catSelection) 
    catSelection.modalPresentationStyle = .custom 
    catSelection.modalTransitionStyle = .crossDissolve 
    self.present(catSelection, animated: true, completion: nil) 
} 

LiveDealCategorySelection.swift

func setupBackgroundView(){ 
    if !UIAccessibilityIsReduceTransparencyEnabled() { 
     self.view.backgroundColor = .clear 
     let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark) 
     let blurEffectView = UIVisualEffectView(effect: blurEffect) 
     blurEffectView.frame = (self.view?.bounds)! 
     blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
     self.view?.addSubview(blurEffectView) 
    } else { 
     self.collectionView?.backgroundColor = UIColor.black 
    } 
} 

這是結果,你可以看到背後LiveDealCategorySelection的MapView:

enter image description here

問題是,當我嵌入在V一個UINavigationController內觀使用控制器,因爲我不能給它的背景色設置爲.clear

func showLiveDealsCategory(sender:UITextField){ 
     let catSelection = LiveDealCategorySelection() 
     let navContr = UINavigationController(rootViewController: catSelection) 
     catSelection.modalPresentationStyle = .custom 
     catSelection.modalTransitionStyle = .crossDissolve 
     self.present(navContr, animated: true, completion: nil) 
} 

LiveDealCategorySelection我想:

override func viewWillAppear(_ animated: Bool) { 
     if self.navigationController != nil { 
      self.navigationController!.view.backgroundColor = .clear 
      self.navigationController!.view.tintColor = .clear 
     } 
    } 

,我也試圖把背景顏色,當我設置爲.clear實例化導航控制器,但我得到一個黑色的背景。任何想法?

回答

1

你只是忘了將演示文稿樣式移動到UINavigationController

navContr.modalPresentationStyle = .custom 
+0

不幸的是它也無法正常工作。可以使用'.clear'背景不能與'UINavigationController'一起使用 – mat

+0

在模擬器和設備上都有一個非常類似的代碼,它包含'UINavigationController'上的'.clear',只是沒有CollectionView,在iOS10.2上運行。 – MarioBajr

+0

我試過了,它不起作用。奇怪的! – mat

0

隨着導航控制器,你不能達到你想要的。只有Modal的OverCurrentContext呈現,支持從一個視圖控制器到另一個透明或半透明背景的轉換。

如果你想模糊這個視圖控制器的背景,然後使用具有模態呈現呈現這個視圖控制器的「自定義(或過電流情況下)」

別的試試這個:

func showLiveDealsCategory(sender:UITextField){ 
     let catSelection = LiveDealCategorySelection() 
     let navContr = UINavigationController(rootViewController: catSelection) 
     navContr.modalPresentationStyle = .custom // << Mark this update 
     catSelection.modalPresentationStyle = .custom 
     catSelection.modalTransitionStyle = .crossDissolve 
     self.present(navContr, animated: true, completion: nil) 
} 
+0

謝謝。你能否詳細說明一下。你的意思是我可以使用'.overCurrentContext'來呈現navigationController,並且支持清晰的背景? – mat

+0

不是導航控制器,只是您的視圖控制器。 例如from ViewController1 let viewController2 = storyboard.instantiateViewcontrollerWithIndentier as? ViewController2 viewcontroller.modalpresentation = .overcurrentcontext self.present(viewcontroller2,animated:true) – Krunal

+0

它已經在使用上面顯示的代碼中的viewController工作。使用'.custom'而不是'.overCurrentContext' – mat