blurred background in swift with clear subview - ios

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... :)

Related

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.

I want my self.view content should be visible but blurred, propose best way

In storyBoard, I added separate UIView as First responder in view controller.Then I added that view as subview of self.view. But I want self.view should be blurred when subview is shown. How can i do this?
I tried by applying
let imageView = UIImageView(image: UIImage(named: "blur2.jpg"))
imageView.frame = self.view.bounds
imageView.contentMode = .scaleAspectFit
self.view.addSubview(imageView)
let blurEffect = UIBlurEffect(style: .regular)
let blurredEffectView = UIVisualEffectView(effect: blurEffect)
blurredEffectView.frame = imageView.bounds
self.view.addSubview(blurredEffectView)
self.view.addSubview(view_Message)
view_Message.center = self.view.center
view_Message.alpha = 1
view_Message.layer.cornerRadius = 0.3
view_Message.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
view_Message is UIView which is taken as FirstResponder in storyBoard.
But my self.view content goes invisible. I want My self.view should get blur(but should be shown lightly) at the same time I want my subview is shown.
i suggest you to
1. Take a UIView(let say myView) of frame self.view.bounds with clear as backgroundcolor
2. Add imageView into it of same frame
3. set backgroundcolor for imageView white,alpha nearly 0.7,and blur effect.
4. Now add your view_Message into myView as SubView.
5. Atlast add myView to self.view
or you can go by using UIVisualEffectBlur class either in storyboard or programmatically available for iOS 8+.
You can also use visualEffectView rather than blurEffect like this:
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = view.bounds
visualEffectView.alpha = 0.7
self.view.addSubview(visualEffectView)
You can do like this
let frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height )
blurview = UIView.init(frame: frame)
blurview.backgroundColor = UIColor.blackColor()
blurview.layer.opacity = 0.5
self.view.addSubview(blurview)
Check 3D view. It seems That your ImageView is On Top Of all views. This will hide all your views behind it.
Try
self.view.sendSubviewToBack(imageView)
at the end, This will send imagview at back an your all subviews will be visible.

UICollectionView vibrancy effect between cells

I have a UICollectionView with 3 cells (cells' width is self.collectionView.frame.size.width / 3).
The collectionView is placed inside a UIVisualEffectView with dark blur effect.
I want to add a 1px vibrancy gap between every cell, I've tried:
let divider1 = UIVisualEffectView(frame: CGRect(x: self.collectionView.frame.size.width / 3, y: 0, width: 1, height: self.collectionView.frame.size.height))
divider1.effect = UIVibrancyEffect(blurEffect: UIBlurEffect(style: .dark))
let divider2 = UIVisualEffectView(frame: CGRect(x: (self.collectionView.frame.size.width / 3) * 2, y: 0, width: 1, height: self.collectionView.frame.size.height))
divider2.effect = UIVibrancyEffect(blurEffect: UIBlurEffect(style: .dark))
self.collectionView.addSubview(divider1)
self.collectionView.addSubview(divider2)
but it doesn't present anything. If I set the divider's backgroundColor to a color it does work, so it's probably something with the vibrancy effect.
Any idea why?
Thanks!
Try this code:
Note: Tested in Swift 3
override func viewDidLoad() {
super.viewDidLoad()
// Add a background view to the collectionView
let backgroundImage = UIImage(named: "Set your image file here")
let imageView = UIImageView(image: backgroundImage)
self.collectionView.backgroundView = imageView
//To get a balance look from collectionView background
collectionView.backgroundColor = UIColor(patternImage: backgroundImage!)
// To setup a blurView background
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = view.bounds
imageView.addSubview(blurView)
}
Output:
To make it works you need to create UIVibrancyEffect using blur effect you use in your collection view.

UIImage doesn't fade in/out when blur is on

I have UIImage which is blurred like this:
myImageView.image = UIImage(named: imageSet[0])
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView?.frame = view.bounds
myImageView.addSubview(blurEffectView!)
and then I want to fade in new UIImage like this:
UIView.transitionWithView(self.backgroundImageView,
duration:1.0,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: { self.myImageView.image = UIImage(named: self.imageSet[randomInt]
},
completion: nil)
and the problem is, new image simply appears, rather than fade in. If I disable blur, images fade in and out. What I am doing wrong?
You add your blurEffectView as a subview of myImageView but you can not change alpha or give animation to UIBlurEffect nor UIVisualEffectView. Instead of that, add new UIView with same frame and constraints as your imageView object then add your effects as a subview of a UIView object ;
myImageView.image = UIImage(named: imageSet[0])
let animationView = UIView(frame: myImageView.bounds)
animationView.backgroundColor = .clearColor()
blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style:.Light))
blurEffectView?.frame = animationView.bounds
view.addSubview(animationView)
animationView.addSubview(blurEffectView!)

How to remove blur subview from super view

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

Resources