swift - Add blur to UIView in order to blur the Controller below? Read before
this is what I've done
class ViewController: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate {
var pickerDataSource = ["White", "Red", "Green", "Blue"];
@IBOutlet var pickerView: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.regular)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.pickerView.delegate = self
self.pickerView.dataSource = self
self.view.addSubview(blurEffectView)
self.view.bringSubview(toFront: self.pickerView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count;
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerDataSource[row]
}
}
How to apply blur to a UIView?
Ever since the release of iOS 7, This question has been getting a lot of hits and I thought I should follow up on what I ended up doing.
I personally blurred the picture at the time with photoshop, but there are many great third party libraries available that do dynamic and static blurring, here are a couple:
- This ingenious library takes a layer from
UITabBar
and applies to
any view to get iOS 7's native blur:
https://github.com/JagCesar/iOS-blur/ - This library is a real time blurring library that live blurs any view
below it: https://github.com/alexdrone/ios-realtimeblur (as pointed out by qegal) - Another one that does the same realtime blur:
https://github.com/nicklockwood/FXBlurView - This Library is the one I personally use and is Great, allowing
you to set the tint color among other settings:
https://github.com/ivoleko/ILTranslucentView
I wanted to post this because you no longer need to take screenshots of individual frames or screen.
iOS 8: With the coming release of iOS 8, Apple has included a built in blurred UIView
, UIVisualEffectView
(https://developer.apple.com/documentation/uikit/uivisualeffectview)
Adding blur effect to background in swift
I have tested this code and it's working fine:
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
For Swift 3.0:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
For Swift 4.0:
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
Here you can see result:
Or you can use this lib for that:
https://github.com/FlexMonkey/Blurable
How create transparent view with with blur effect?
I found an excellent tutorial that might help you to create a smooth transparent view with blur effect: https://www.raywenderlich.com/167-uivisualeffectview-tutorial-getting-started
How do I correctly blur a UIView using a UIVisualEffectView?
Try changing this line: backgroundView.addSubview(blurEffectView)
to this: backgroundView.insertSubview(blurEffectView, at: 0)
Creating a blurring overlay view
You can use UIVisualEffectView
to achieve this effect. This is a native API that has been fine-tuned for performance and great battery life, plus it's easy to implement.
Swift:
//only apply the blur if the user hasn't disabled transparency effects
if !UIAccessibility.isReduceTransparencyEnabled {
view.backgroundColor = .clear
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
//always fill the view
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView) //if you have more UIViews, use an insertSubview API to place it where needed
} else {
view.backgroundColor = .black
}
Objective-C:
//only apply the blur if the user hasn't disabled transparency effects
if (!UIAccessibilityIsReduceTransparencyEnabled()) {
self.view.backgroundColor = [UIColor clearColor];
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//always fill the view
blurEffectView.frame = self.view.bounds;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:blurEffectView]; //if you have more UIViews, use an insertSubview API to place it where needed
} else {
self.view.backgroundColor = [UIColor blackColor];
}
If you are presenting this view controller modally to blur the underlying content, you'll need to set the modal presentation style to Over Current Context and set the background color to clear to ensure the underlying view controller will remain visible once this is presented overtop.
Is there a method to blur a background in SwiftUI?
1. The Native SwiftUI way:
Just add .blur()
modifier on anything you need to be blurry like:
Image("BG")
.blur(radius: 20)
Note the top and bottom of the view
Note that you can Group
multiple views and blur them together.
2. The Visual Effect View:
You can bring the prefect UIVisualEffectView
from the UIKit:
VisualEffectView(effect: UIBlurEffect(style: .dark))
With this tiny struct:
struct VisualEffectView: UIViewRepresentable {
var effect: UIVisualEffect?
func makeUIView(context: UIViewRepresentableContext) -> UIVisualEffectView { UIVisualEffectView() }
func updateUIView(_ uiView: UIVisualEffectView, context: UIViewRepresentableContext) { uiView.effect = effect }
}
3. iOS 15: Materials
You can use iOS predefined materials with one line code:
.background(.ultraThinMaterial)
Related Topics
How to Dismiss the Keyboard When Editing a Uitextfield
Drop-Down List in Uitableview in iOS
Exhaustive Condition of Switch Case in Swift
Retrieve Custom Prototype Cell Height from Storyboard
Converting iPhone Xib to iPad Xib
How to Obtain a Dynamic Table View Section Header Height Using Auto Layout
Toggling Privacy Settings Will Kill the App
How to Run an Action When a State Changes
Volunteermatch API Objective C
How to Assign an Action for Uiimageview Object in Swift
Wrong Color in Interface Builder
Callback Method If User Declines Push Notification Prompt
Objective C: Downloading File with Progress Bar
How to Achieve a "Clock Wipe"/ Radial Wipe Effect in iOS
How to Create a Category in Xcode 6 or Higher
Xcode 9 Swift Language Version (Swift_Version)
Uipopoverpresentationcontroller on iPhone Doesn't Produce Popover