【问题标题】:How to implement UIVisualEffectView in UITableView with adaptive segues如何在 UITableView 中使用自适应 segues 实现 UIVisualEffectView
【发布时间】:2014-08-16 22:20:49
【问题描述】:

我想实现UIVisualEffectView 以对视图应用模糊效果以显示位于其后面的视图。

这个背景应该模糊的视图是一个嵌入在UINavigationController中的UITableViewController,它要么在iPad上以弹出框的形式呈现,要么在iPhone上以全屏模式呈现,这要归功于iOS 8 个自适应转场(以 Popover 形式呈现)。当这个视图控制器在弹出窗口中时,我希望背景模糊弹出窗口下方的内容,当它全屏显示时,我希望背景模糊前一个视图控制器。

我已经尝试过实现这一点,但没有成功。我什至无法让模糊效果为弹出框工作。我认为这段代码应该可以解决问题:

//In viewDidLoad on the UITableViewController subclass:
let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
effectView.frame = tableView.frame
tableView.addSubview(effectView)

我还尝试将子视图添加到tableView.backgroundView,我尝试将backgroundView 设置为我的effectView,我尝试使用自动布局约束而不是设置框架,但没有任何效果。你能帮我完成想要的行为吗?

我试图获得的一个例子:
iPad弹出框:

iPhone 模态演示:

【问题讨论】:

    标签: ios uitableview swift uipopovercontroller modalviewcontroller


    【解决方案1】:

    我终于找到了解决办法。我必须创建两个单独的 segue - 一个用于仅在 iPad 上调用的 Popover Presentation,另一个用于 iPhone 的 Presentation Style 设置为 Over Full Screen(这很重要)的模态 segue。

    在显示的表格视图控制器中,viewDidLoad 中,此代码将应用所需的模糊效果(以及奖励,前提是它们没有禁用透明效果):

    if (!UIAccessibilityIsReduceTransparencyEnabled()) {
        tableView.backgroundColor = UIColor.clear
        let blurEffect = UIBlurEffect(style: .light)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        tableView.backgroundView = blurEffectView
    
        //if inside a popover
        if let popover = navigationController?.popoverPresentationController {
            popover.backgroundColor = UIColor.clear
        }
    
        //if you want translucent vibrant table view separator lines
        tableView.separatorEffect = UIVibrancyEffect(blurEffect: blurEffect)
    }
    

    这会使表格背景看起来就像在屏幕截图中一样。 iPhone 的诀窍是确保它显示在屏幕上,而 iPad 的诀窍是删除 popoverPresentationController 中的 backgroundColor

    【讨论】:

    • 我没有看到“全屏显示”的演示风格。只有 iOS 8 吗?
    • @livingtech 是的。自适应转场(以及解决方案中使用的其他 API)仅适用于 iOS 8+。
    • 我还注意到 iPad 2 和 iPad Retina 模拟器不显示模糊,它使视图仅半透明。所有其他模拟器都可以正常工作,包括 iPhone 4s。只是不要被这个抓住,因为我首先认为 UIVisualEffectView 不起作用
    • 细胞呢?
    • @yar1vn 随时添加答案
    【解决方案2】:

    使用自适应添加模糊的简单示例:

    extension ViewController: UIPopoverPresentationControllerDelegate {
    
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        // Grab the destination view controller and set the presentation delegate
        let viewController = segue.destinationViewController as UIViewController
        viewController.popoverPresentationController?.delegate = self
        viewController.presentationController?.delegate = self
      }
    
      // Note: Only called for FormSheet and Popover
      func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        switch controller.presentedViewController {
        case let vc as PopoverViewController:
          return .None // Keep the popover in Compact screens
        default:
          return .OverFullScreen
        }
      }
    
      func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
        // This is a custom method on UIViewController
        controller.presentedViewController.createBlur()
    
        // Wrap in a Navigation Controller, the controller should add a title and bar buttons
        if !(presentedViewController is UINavigationController) {
          return UINavigationController(rootViewController: presentedViewController)
        }
      }
    }
    
    extension UIViewController {
      func createBlur(effectStyle: UIBlurEffectStyle = .Light) {
        if !UIAccessibilityIsReduceTransparencyEnabled() {
          view.backgroundColor = UIColor.clearColor()
    
          let blurView = UIVisualEffectView(effect: UIBlurEffect(style: effectStyle))
          blurView.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
          blurView.frame = view.bounds
    
          view.insertSubview(blurView, atIndex: 0)
        }
      }
    }
    
    extension UITableViewController {
      override func createBlur(effectStyle: UIBlurEffectStyle = defaultBlurEffectStyle) {
        if !UIAccessibilityIsReduceTransparencyEnabled() {
          tableView.backgroundColor = UIColor.clearColor()
    
          let blurEffect = UIBlurEffect(style: effectStyle)      
          tableView.backgroundView = UIVisualEffectView(effect: blurEffect)
          tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: blurEffect)
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      这个有点晚了,但对我来说最好的解决方案(在 ios8 + 中)是在 Storyboard 中使用 UIVisualEffectView 并将其作为我的 ViewController 的根视图。然后将我的 tableview 添加到那个

      要找到视觉效果视图,请转到右下角的组件选择器(不确定它叫什么)并搜索 VisualEffectView

      这似乎是一种更简单的方法,并且符合 Apple 的建议,即尽可能多地在 Storyboard 上进行操作

      【讨论】:

        【解决方案4】:

        IOS 10 Swift 3 的小改动

        if #available(iOS 10.0, *) {
                let blurEffect = UIBlurEffect(style: .prominent)
                let blurEffectView = UIVisualEffectView(effect: blurEffect)
                groupTable.backgroundView = blurEffectView
        
            } else {
                let blurEffect = UIBlurEffect(style: .dark)
                let blurEffectView = UIVisualEffectView(effect: blurEffect)
                groupTable.backgroundView = blurEffectView
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-04
          • 2016-10-04
          • 1970-01-01
          相关资源
          最近更新 更多