Joey Joey - 11 months ago 302
iOS Question

How to implement UIVisualEffectView in UITableView with adaptive segues

I would like to implement

to apply a blur effect to a view to show the view that lies behind it.

This view that should have its background blurred is a
that is embedded in a
, 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

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

An example of what I am trying to obtain:

iPad popover:

enter image description here

iPhone modal presentation:

enter image description here


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.

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.clearColor()
    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.clearColor()

    //if you want translucent vibrant table view separator lines
    tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: blurEffect)

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.