如何在UITableView中使用自适应segue实现UIVisualEffectView [英] How to implement UIVisualEffectView in UITableView with adaptive segues

查看:157
本文介绍了如何在UITableView中使用自适应segue实现UIVisualEffectView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to implement UIVisualEffectView to apply a blur effect to a view to show the view that lies behind it.

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

This view that should have its background blurred is a UITableViewController that is embedded in a UINavigationController, and it will either be presented in a popover on iPad or it will be presented full screen modally on iPhone, thanks to iOS 8 adaptive segues (Present as Popover). When this view controller is in a popover I want the background to blur what's underneath the popover, and when it's presented full screen I want the background to blur the previous view controller.

我试图实现这个并且没有成功。我甚至无法使模糊效果适用于弹出窗口。我认为这段代码可以解决这个问题:

I have tried to implement this and have not been successful. I cannot even get the blur effect to work for the popover. I thought this code should do the trick:

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

我也尝试将子视图添加到 tableView.backgroundView ,我尝试设置 backgroundView 到我的 effectView ,我尝试使用Autolayout约束而不是设置框架,但没有任何效果。你能帮助我完成所期望的行为吗?

I also tried adding the subview to the tableView.backgroundView, I tried setting the backgroundView to my effectView, I tried using Autolayout constraints instead of setting the frame, but nothing has worked. Can you help me accomplish the desired behavior?

我想要获得的一个例子:

iPad popover:

An example of what I am trying to obtain:
iPad popover:

iPhone模态演示:

iPhone modal presentation:

推荐答案

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

I've finally found a solution. I had to create two separate segues - one for a Popover Presentation which is called only on iPad, and the other a modal segue with Presentation Style set to Over Full Screen (that's important) for iPhone.

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

In the table view controller that is being presented, in viewDidLoad, this code will apply the desired blur effect (and bonus, only if they haven't disabled transparency effects):

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

This causes the table background to appear just like it does in the screenshots. The trick for iPhone was to ensure it's presented over the screen, while the trick for iPad was to remove the backgroundColor in the popoverPresentationController.

这篇关于如何在UITableView中使用自适应segue实现UIVisualEffectView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆