Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIVisualEffectView in iOS 10

I am presenting a UIViewController that contains a UIVisualEffectView as follows:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {     [self performSegueWithIdentifier:@"segueBlur" sender:nil]; }  -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {     if([segue.identifier isEqualToString:@"segueBlur"]) {         ((UIViewController *)segue.destinationViewController).providesPresentationContextTransitionStyle = YES;         ((UIViewController *)segue.destinationViewController).definesPresentationContext = YES;         ((UIViewController *)segue.destinationViewController).modalPresentationStyle = UIModalPresentationOverFullScreen;     } } 

As you can see, I'm using the UIModalPresentationStyleOverFullScreen so that when the view controller with blur appears, the blur will be 'applied' to the content of the view controller that is presenting it; the segue has a Cross Dissolve transition style.

The effect looks as expected. However, in iOS 9 the presentation is smoother than in iOS 10. In iOS 10 when the view controller appears it seems like a 2-step animation, while in iOS 9 the blur is applied immediately.

A picture is worth a thousand words so I uploaded a video showing this strange behavior:

UIVisualEffectView iOS 9 vs iOS 10

My question is: How can I present the view controller in iOS 10 as it is presented in iOS 9?

like image 238
Axort Avatar asked Sep 24 '16 00:09

Axort


People also ask

What is the purpose of UIVisualEffectView?

The UIVisualEffectView provides a simple abstraction over complex visual effects. Depending on the desired effect, the results may affect content layered behind the view or content added to the view's contentView. Apply a UIVisualEffectView to an existing view to apply a blur or vibrancy effect to the exiting view.

How do you blur the background in IOS Swift?

You will need to create the blur effect using the UIBlurEffect Class in the viewDidLoad method. After that you'll need to apply the effect to the UIVisualEffectView. The UIVisualEffectView needs to have the container dimensions defined, and it will be the first view of the stack of the view controller.

How do you add a blur to a view in Swift?

To get an easy blue effect all we have to do is get an instance of UIBlurEffect and assign it to an instance of UIVisualEffectView as the effect. Then we just add the instance of UIVisualEffectView as a subview to our UIView or UIImageView.


1 Answers

iOS 10 has changed the way UIVisualEffectView works, and it has broken many use cases which were not strictly speaking "legal", but worked before. Sticking to documentation, you should not be fading in UIVisualEffectView, which is what happens when you use UIModalTransitionStyleCrossDissolve. It seems now broken on iOS 10, along with masking of visual effect views, and others.

In your case, I would suggest an easy fix which will also create a better effect than before, and is supported on both iOS 9 and 10. Create a custom presentation and instead of fading the view in, animate the effect property from nil to the blur effect. You can fade in the rest of your view hierarchy if needed. This will neatly animate the blur radius similar to how it looks when you pull the home screen icons down.

like image 133
Léo Natan Avatar answered Oct 03 '22 01:10

Léo Natan