How to remove blur subview from super view - ios

I need to blur screen when alert is shown, so I googled the function, which blurs the screen
it looks like
var effectView: UIVisualEffectView!
func addBlur() {
var effect = UIBlurEffect(style: UIBlurEffectStyle.Light)
effectView = UIVisualEffectView(effect: effect)
effectView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
view.addSubview(effectView)
}
I want to remove the blur after user dismissed the alert and I come up with the such function
func removeBlur() {
effectView.view.removeFromSuperview()
}
but it doesn't work, says UIVisualEffectView does not have a member named "view"
How to fix it?

func removeBlur() {
effectView.removeFromSuperview()
}

You can also try this way:
func blureffect() {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
}

Related

Blur effect on view background

I have this extension, and I was hoping for a blur background, where I can see blur through the view:
import Foundation
import UIKit
extension UIView
{
func addBlurEffect()
{
if !UIAccessibilityIsReduceTransparencyEnabled() {
self.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(blurEffectView)
} else {
self.backgroundColor = UIColor.white
}
}
}
Then I use it like this:
let v = UIView(frame: self.view.frame)
v.addBlurEffect()
self.view.addSubview(v)
But I can not see anything under the view?
Add this framework to your project.
Add transparent view over your background and setup blur (view Readme on github)
Preview

Blur effect not appearing in UITableViewCell

I'm trying to add a blur effect to my view. I created the blur effect like so
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
return blurEffectView
}()
Then I add it to the subview like so, and set up the constraints with it as well
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
let view = tableViewCell
addSubview(view)
view.addSubview(blurLabel)
blurLabel.leftAnchor.constraint(equalTo: self.leftAnchor,constant:0).isActive = true
//blurLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 178).isActive = true
blurLabel.widthAnchor.constraint(equalToConstant: 375).isActive = true
blurLabel.heightAnchor.constraint(equalToConstant: 180).isActive = true
But when I run the application it doesn't appear at all
Edit: As suggested by #HAS & #Fogmeister, you can also use translatesAutoResizingMaskIntoConstraints = false as it is better solution to use Auto Layout without specifying explicit frames.
You will have to assign frame to UIVisualEffectView like this:
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = CGRect(x: 0, y: 0, width: 60, height: 40) //give desirable frame
return blurEffectView
}()
If you would like, instead of giving explicit frames you can also provide your blurView's to any of your view or, subviews.
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = myMainview.bounds //Blur effect's frame
return blurEffectView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Use it somewhere using
self.view.insertSubview(blurEffectView, belowSubview: myAnotherView)
// You may also use any of the subviews too, instead of the self.view
/// self.myView.insertSubview(blurEffectView, belowSubview: myAnotherView)
}
To fix this you just have to stop it automatically creating constraints. Like this...
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
// this line will stop constraints being added for a zero frame
blurEffectView.translatesAutoResizingMaskIntoConstraints = false
return blurEffectView
}()

Blur behind UIImage not going away after image is dismissed

I've been trying to add a blur effect behind an image after I tap on it, however, it is not going away after I dismiss the image. The blur doesn't disappear.
func didTapImageView(_ sender: UITapGestureRecognizer) {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.frame
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.layer.masksToBounds = true
//newImageView.backgroundColor = UIColor.black.withAlphaComponent(1)
newImageView.contentMode = UIViewContentMode.scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
//blurEffectView.addGestureRecognizer(tap)
self.view.addSubview(blurEffectView)
self.view.addSubview(newImageView)
}
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
How can I remove the blur after the tap? and also, how do I make it full screen? it doesn't seem to be working that either.
It looks to me like when you call dismissFullScreenImage you are only removing the UIImageView from the superview. The blurEffectView is a separate subview entirely. You'll have to set a property for the effect view and then remove it when you call for the image to be removed. At the top of your class say something like like:
var blurEffectView:UIVisualEffectView!
Then in your didTapImageView:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.frame
Then when you call dismissFullScreenImage, you need to say something like :
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
blurEffectView.removeFromSuperview()
}
I don't know what you mean "how do I make it fullscreen?" .. The visual effect already is fullscreen. At least it looks that way on your screenshot.

UIVisualEffect is not showing on UIView

I have a UIView and that a create programmatically, then I added as a subView to my viewController.view. However, The view is not affected with the blur.
Here is my code:
let blurryView: UIView = {
let blur = UIView()
blur.backgroundColor = UIColor.yellow
blur.alpha = 0.90
let blurEffect = UIBlurEffect(style: .extraLight)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blur.addSubview(blurEffectView)
blurEffectView.center = blur.center
blurEffectView.frame = blur.bounds
blur.translatesAutoresizingMaskIntoConstraints = false
return blur
}()
func manageBlurView() {
blurryView.widthAnchor.constraint(equalToConstant: 250).isActive = true
blurryView.heightAnchor.constraint(equalToConstant: 250).isActive = true
blurryView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
blurryView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(blurryView)
manageBlurView()
}
The blur effect is added and I doubled checked that using breakpoints and pint statements if blur.subviews == [blurEffectView] {
print("Blur effect added as a subview")
} and it printed.
I have a background image to make sure if it's blurred.
I also tried to add the blur effect in the viewDidLoad(), but nothing happened.
Add both the yellow background view and the blurred view directly as child of your main view :
let backg = UIView(frame: self.view.frame)
backg.backgroundColor = UIColor.yellow
backg.alpha = 0.90
self.view.addSubview(backg)
// the blur effect
let blurEffect = UIBlurEffect(style: .extraLight)
let blurredEffectView = UIVisualEffectView(effect: blurEffect)
blurredEffectView.frame = self.view.frame
self.view.addSubview(blurredEffectView)
You could put the above code in viewDidLoad.

how to get a blurred effect behind my UINavigationBar

I have been trying to get an effect of having my scrollView appear under the navigationBar slightly blurred as is the case in the apple messages app. I have tried the following methods and none of them has worked:
1:
self.navigationController?.navigationBar.isTranslucent = true
2:
self.navigationController?.navigationBar.barStyle = UIBarStyle.blackTranslucent
3:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
4:
self.navigationController?.navigationBar.backgroundColor = UIColor.clear()
I have read many posts with variations of the above methods and have tried most of them but to no avail. I thought the point of setting isTranslucent = true was to get exactly that effect. Is there something else I should be trying? Basically I just want my navigationBar to be slightly see-through.
UIVisualEffectView is what you are looking for, and well demonstrated here:
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
visualEffectView.frame = bounds
self.navigationController?.navigationBar.addSubview(visualEffectView)
That said, use your method 1 and 2 together should also work:
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.topItem.title = #"Contacts";
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
Here you can add this code for your navigation bar blur effect this function is perfect for you
func navigationBlurEffect() {
// Add blur view
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
visualEffectView.frame = bounds
visualEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
//here you can choose one
self.navigationController?.navigationBar.addSubview(visualEffectView)
// Or
/*
If you find that after adding blur effect on navigationBar, navigation buttons are not visible then add below line after adding blurView code.
*/
self.navigationController?.navigationBar.sendSubviewToBack(visualEffectView)
// Here you can add visual effects to any UIView control.
// Replace custom view with navigation bar in above code to add effects to custom view.
}
//**This is only for Image Blur code
And this code you can use for Image Blur effect
func getImageBlur(image:UIImageView){
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = (image.bounds)
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
image.addSubview(blurEffectView)
}
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = (self.navigationController?.navigationBar.bounds)!
self.navigationController?.navigationBar.addSubview(blurEffectView)

Resources