【问题标题】:UINavigationController - clear background using UIBlurEffectUINavigationController - 使用 UIBlurEffect 清除背景
【发布时间】:2017-02-23 17:15:48
【问题描述】:

我在UIViewController 中使用UIBlurEffect,它以.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的结果:

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

【问题讨论】:

  • 您找到解决方案了吗?

标签: ios swift swift3 uinavigationcontroller uiblureffect


【解决方案1】:

您只是忘记将演示样式移动到 UINavigationController

navContr.modalPresentationStyle = .custom

【讨论】:

  • 不幸的是它也不起作用。显然.clear 背景不能与UINavigationController 一起使用
  • 有一个与您发布的代码非常相似的代码,其中包括UINavigationController 上的.clear,只是没有CollectionView,在模拟器和设备上的iOS10.2 上运行。
【解决方案2】:

使用导航控制器,您无法实现您想要的。只有带有 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)
}

【讨论】:

  • 谢谢。你能详细说明一下吗。你的意思是我可以使用.overCurrentContext 呈现navigationController 并且支持清晰的背景?
  • 不是导航控制器,而是您的视图控制器。例如从 ViewController1 让 viewController2 = storyboard.instantiateViewcontrollerWithIndentier 作为? ViewController2 viewcontroller.modalpresentation = .overcurrentcontext self.present(viewcontroller2, animated: true)
  • 它已经在使用上面显示的代码中的 viewController 工作了。使用.custom 而不是.overCurrentContext
  • 那么,你只想要导航控制器吗?
  • 不幸的是我需要一个navigationController,留下透明背景。
猜你喜欢
  • 1970-01-01
  • 2021-11-28
  • 2013-09-16
  • 1970-01-01
  • 2011-05-12
  • 2012-06-06
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
相关资源
最近更新 更多