UIVisualEffect is not showing on UIView - ios

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.

Related

How can I make the Navigation Bar transparent?

How can I make this part of the app (the Navigation Bar) translucent? I don't want it to be fully transparent, I want it to show the website content's colours and be blurred.
Link with screenshot: iPhone 12 simulator with Navigation Bar Opaque
I have looked at tens of possibilities and none worked:
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
visualEffectView.frame = bounds
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController.navigationBar.translucent = YES;
I also tried:
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = (self.navigationController?.navigationBar.bounds)!
self.navigationController?.navigationBar.addSubview(blurEffectView)darkUIBlurEffect.Style
Also this:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) //UIImage.init(named: "transparent.png")
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
And this:
self.navigationController?.navigationBar.isTranslucent = true
I also tried using the Main.storyboard's Attributes Inspector and ticked Translucent.
Attributes Inspector tab on Navigation Bar in Main.storyboard
No method worked. What should I do?
Try this:
extension UIViewController {
func blurNav(withAlpha alpha:CGFloat = 1.0 ) {
let statusBarHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
let rect = navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight)) ?? .zero
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
blurView.frame = rect
blurView.isUserInteractionEnabled = false
blurView.alpha = alpha
blurView.layer.zPosition = -1
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.addSubview(blurView)
navigationController?.navigationBar.sendSubviewToBack(blurView)
}
}
Usage:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
blurNav(withAlpha: 0.8)
}

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

blurred background in swift with clear subview

i want a blurred background. i have a subview for the main view . and i want the subview to not be affected by blur. What should i do?
Code:
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
vibrancyEffectView.frame = view.bounds
view.addSubview(blurEffectView)
view.addSubview(vibrancyEffectView)
FrameView = UIView()
if let FrameView = FrameView {
FrameView.layer.borderColor = UIColor.white.cgColor
FrameView.layer.borderWidth = 2
FrameView.frame=CGRect(x:30,y:(view.frame.height/2-50),width:view.frame.width-60,height:100)
blurEffectView.contentView.addSubview(FrameView)
}
So i want the view to be blurred and the FrameView to be clear.
This is what my app currently looks like.
I had try a test project , and i add the blur to mainView first , then i add the frameView to mainView , i seems good and the frameView is not blur.
let mainView = UIImageView.init(frame: self.view.bounds);
mainView.image = UIImage.init(named: "a.png");
let blurEffect = UIBlurEffect.init(style: .dark);
let effectView = UIVisualEffectView.init(effect: blurEffect);
effectView.frame = mainView.bounds;
effectView.alpha = 1;
let frameView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 50));
frameView.image = UIImage.init(named: "b.png");
frameView.center = self.view.center;
//add the blur view first
mainView.addSubview(effectView);
self.view.addSubview(mainView);
self.view.addSubview(frameView);
You have to add the frame upper in you hiearchy, as you see, the BlurEffectView blurs every subview within it, so check that you are adding the subView to the main View of viewController and use func bringSubView to front... :)

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
}()

Background image not loading on device

I have this code that places the image in the background and applies a blur effect:
let effect = UIBlurEffect(style: .Dark)
override func viewDidLoad() {
let backgroundView = UIView(frame: view.bounds)
backgroundView.autoresizingMask = resizingMask
backgroundView.addSubview(self.buildImageView())
backgroundView.addSubview(self.buildBlurView())
tableView.backgroundColor = UIColor.clearColor()
tableView.backgroundView = backgroundView
tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)
}
func buildImageView() -> UIImageView {
let imageView = UIImageView(image: UIImage(named: "pexels-photo"))
imageView.frame = view.bounds
imageView.autoresizingMask = resizingMask
return imageView
}
func buildBlurView() -> UIVisualEffectView {
let blurView = UIVisualEffectView(effect: effect)
blurView.frame = view.bounds
blurView.autoresizingMask = resizingMask
return blurView
}
When I run this in the simulator it loads fine. But when I test it on an iPhone 6 it just loads a black background.
Any help on how to solve this problem would be appreciated.
Try below code tested in Xcode 8 it worked. Code tested as a UIView..
**Answer 1:** From your code
// Change bLurView Style to .dark or .extraLight If you want
let effect = UIBlurEffect(style: .light)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let backgroundView = UIView(frame: view.bounds)
// backgroundView.autoresizingMask = resizingMask
view.addSubview(self.buildImageView())
view.addSubview(self.buildBlurView())
/////Code tested in UIView not in tableView.It won't do any difference just let you know///////
tableView.backgroundColor = UIColor.clearColor()
// tableView.backgroundView = backgroundView
tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)
/////////////////////////////////////////////////////////////////////
}
func buildImageView() -> UIImageView {
let imageView = UIImageView(image: UIImage(named: "pexels-photo"))
imageView.frame = view.bounds
// imageView.autoresizingMask = resizingMask
return imageView
}
func buildBlurView() -> UIVisualEffectView {
let blurView = UIVisualEffectView(effect: effect)
blurView.frame = view.bounds
// blurView.autoresizingMask = resizingMask
return blurView
}
Answer 2:
override func viewDidLoad() {
super.viewDidLoad()
// Add a background view to the table view
let backgroundImage = UIImage(named: "your image file")
let imageView = UIImageView(image: backgroundImage)
self.tableView.backgroundView = imageView
imageView.contentMode = .ScaleAspectFit
// no lines where there aren't cells
tableView.tableFooterView = UIView(frame: CGRectZero)
//set background colour to light color or clear color to get a transparent look
tableView.backgroundColor = .lightGrayColor()
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = imageView.bounds
imageView.addSubview(blurView)
}
If we want to make the table view cells totally transparent you can just set their background color to clear:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor(white: 1, alpha: 0.5) // UIcolor.clear
}
output of the above code as below....

Resources