UINavigationController - 使用UIBlurEffect清除背景

时间:2017-02-23 17:15:49

标签: ios swift swift3 uinavigationcontroller uiblureffect

我在UIBlurEffect中使用UIViewController,其中显示了.crossDissolve转换样式。 UIViewController在其视图中添加了一个collectionView,因此它们都具有清晰的背景。

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

问题是当我将视图控制器嵌入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,但我得到了黑色背景。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您忘了将演示文稿样式移动到UINavigationController

navContr.modalPresentationStyle = .custom

答案 1 :(得分:0)

使用导航控制器无法达到您想要的效果。只有使用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)
}