我想实现 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 弹出窗口:
iPhone模态呈现:
最佳答案
我终于找到了解决方案。我必须创建两个单独的 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/