I am trying to create a popup menu over a blurred background. I created the function below to blur the view which works great, but when I try to add the menu, it gets blurred as well. I suppose I could take a snapshot of my view and put that under my popup, but I'm guessing there's a better way...
func blurView() {
let blurEffect = UIBlurEffect(style: .Light)
let effectView = UIVisualEffectView(effect: blurEffect)
effectView.frame = view.frame
view.addSubview(effectView)
effectView.alpha = 0
UIView.animateWithDuration(2.0) { effectView.alpha = 0.8 }
}
If you are just unhiding a previously added menu view to show the menu, then the blurView (which was just added) is probably in front of your menu view as well as everything else. After you add the blurView, when you want to show the menu, try something like:
menuView.hidden = false
view.bringSubviewToFront(menuView)
That should move the menu in front of the effect view so it doesn't get blurred.
Related
A UIViewController I'm presenting has a UIImageView, namely backgroundImageView, to be the VC's background and set to fill the whole screen.
I want the UIImageView to be presented with a dark blur effect provided by iOS but the following sizing issue temporarily occurs(on device but not on the simulator) when I first present the view controller:
Inside viewDidLoad():
// `locationImage` is passed during the segue
backgroundImageView.image = locationImage
backgroundImageView.contentMode = .scaleAspectFill
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = backgroundImageView.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
backgroundImageView.addSubview(blurEffectView)
The above view controller's modalPresentationStyle is set to be as currentContext.
If I temporarily remove the blur effect, I see that the background image I gave the view controller got properly set. However once the blur effect is applied, sizing issues occur momentarily.
What exactly is causing this?
Try to set the frame of blurEffectView inside viewWillLayoutSubviews() or in viewDidLayoutSuviews()
here you can find more information about viewcontroller's lifecycle
Does anyone has problems with the UIBlurEffect on iOS10 ?
For some Reason the background of my button etc just gets a bit transparent and doesn't blurr anymore....
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.ExtraLight)
blurBackgroundView = UIVisualEffectView(effect: blurEffect)
blurBackgroundView.frame = frame
button = UIButton(frame: frame)
blurBackgroundView.layer.masksToBounds = true
backgroundColor = UIColor.clearColor()
addSubview(blurBackgroundView)
addSubview(button)
that's how the code looks....
If I change UIBlurEffectStyle.ExtraLight to UIBlurEffectStyle.Prominent based on the new documentation the Button is just clear... so no color at all!
Add whatever you want not to be blurred to your blurBackgroundView. So instead of:
addSubview(blurBackgroundView)
addSubview(button)
You'll have to:
blurBackgroundView.addSubview(button)
addSubview(blurBackgroundView)
Now every item in the current view, below your blurBackgroundView will get blurred, while your button will stay as it is.
I am setting a background image to a parse login view controller. But also want to add this background How can I achieve this?
Here is the code I am using:
backgroundImage.image = UIImage(named: "Melbourne")
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
view.addSubview(blurEffectView)
The problem is that this blurs everything including all the buttons and username and password fields. What can I do?
It is because you are adding your blur view over all the subviews of the view controllers.
try to insert at specific index in view hierarchy.
I have no proper idea of the view hierarchy of your viewController.
Do it using try&Error method like below.
try below code.
view.insertSubview(blurEffectView, atIndex: 1);
if not works try by increasing the index value 2 and so on.
view.insertSubview(blurEffectView, atIndex: 2);// if not change it to 3
It will surely works with specific value.
This code is called inside of a function that is called programatically. The UITableView that it is inserted over has been put in the UIViewController in a storyboard. The UIViewController is embedded in a navigationController.
let containerView = UIView()
containerView.frame.size.width = self.view.frame.width / 2
containerView.frame.size.height = self.view.frame.width / 2
let blurEffect = UIBlurEffect(style: .Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = containerView.bounds
containerView.center = self.view.center
containerView.addSubview(blurEffectView)
self.view.insertSubview(containerView, aboveSubview: matchTableView)
indicator?.startAnimation()
I'm just trying to add an activity indicator over the tableview. The actual activity indicator shows up just fine but I need to put a background behind it so it can be seen correctly. As soon as any type of subview is added to the UIViewController the table disappears.
Have you tried:
let containerView = UIView()
...
containerView.opaque = false
containerView.backgroundColor = UIColor.clearColor()
The tableView is disappearing because you are inserting the container view
above it .
You nedd to add your activity indictor above the table view , where the activity indicator view frame is less than the table view frame
matchTableView.addSubView(containerView)
Previously I had been adding the subview in a function long after the view rendered itself. The way to get this done was to add the view in viewDidAppear. After that I could manipulate it any way I needed. Any period after viewDidAppear in the life cycle and the tableView disappears. I don't know why or if this is the correct way to avoid that issue but this works as a solution.
Adding it in viewDidLoad does not add it to the render tree. It will be there but it will have been too early for it to display properly.
I have a little problem: I'd like to set an image to background in a TableViewController. I tried this:
class SquadreController: UITableViewController {
override func viewDidLoad()
{
super.viewDidLoad()
var sfondo = UIImage(named: "sfondo")
self.view.backgroundColor = UIColor(patternImage: sfondo!)
}
Then I give the blur effect to the view, like this:
let blur = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = self.view.bounds
view.addSubview(blurView)
But there is a big problem. I'm working in a TableView and the TableView, TableCell, NavigationItem, etc. have all disappeared after I apply the blur. Can someone help me?
Thanks a lot!
Try using view.insertSubView(blurView, atIndex: 0) instead of view.addSubview(blurView) as! UIImageView
This question is an older question and Epic Defeater's response does in a way work, there is a much better way to accomplish this. Instead of adding blurView, you need to override the background view. Specifically you need to override tableView.backgroundView.
let blur = UIBlurEffect(style: .light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = self.view.bounds
tableView.backgroundView = blurView
This will only work if you have set the background color to clear.
We can also add a subview below or above any other views using insertSubview
//Inserting below your subView
self.view.insertSubview(subview1_name, belowSubview: subview2_name)
//Inserting above your subView
self.view.insertSubview(subview1_name, aboveSubview: subview2_name)
/// You can use any of your view, instead of self.view