ios - 如何使用自适应转场在 UITableView 中实现 UIVisualEffectView

标签 ios uitableview swift uipopovercontroller modalviewcontroller

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

这个背景应该模糊的 View 是一个嵌入在 UINavigationController 中的 UITableViewController,它要么显示在 iPad 上的弹出窗口中,要么显示在由于 iOS 8 自适应 segues(Present as Popover),在 iPhone 上以模态方式呈现全屏。当此 View Controller 位于弹出窗口中时,我希望背景模糊弹出窗口下方的内容,而当它全屏显示时,我希望背景模糊前一个 View Controller 。

我尝试过实现这个,但没有成功。我什至无法获得适用于弹出窗口的模糊效果。我认为这段代码应该可以解决问题:

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

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

我试图获得的一个例子:
iPad 弹出窗口:
enter image description here

iPhone模态呈现:
enter image description here

最佳答案

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

在正在呈现的 TableView Controller 中,在 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 - 如何使用自适应转场在 UITableView 中实现 UIVisualEffectView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24424165/

相关文章:

swift - ios8机会性后台获取GET请求

ios - 如何将NSDictionary数据添加到NSMutableArray?

iphone - 如何在不共享 View 的情况下制作通用的 iPhone/iPad 应用程序?

ios - 来自 XIB : contentView width is always the same 的自定义 UITableViewCell

ios - 如何在 UITableView 中使用外部数据源

ios - 面向 Swift 协议(protocol)的网络代码导致 Xcode 崩溃

swift - swift 中带有可选闭包的参数

iphone - 开发滑动和 View 更改 iPhone 应用程序

ios - 相同图像切片在 8 和 8 PLUS 上显示差异很大

iOS - 从容器内嵌入的 UITableView 检索父 View 中的单元格标题