Adding a UIvisualeffectview to Button

Adding a UIVisualEffectView to button

Here is a simple example that you could use in order to insert UIVisualEffectView with vibrancy effect.

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 500, height: 100))
button.setTitle("Visual Effect", forState: .Normal)

let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.blueColor().CGColor, UIColor.redColor().CGColor, UIColor.brownColor().CGColor, UIColor.greenColor().CGColor, UIColor.magentaColor().CGColor, UIColor.purpleColor().CGColor, UIColor.cyanColor().CGColor]
gradientLayer.locations = [0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1]
button.layer.insertSublayer(gradientLayer, atIndex: 0)
gradientLayer.frame = button.bounds

let containerEffect = UIBlurEffect(style: .Dark)
let containerView = UIVisualEffectView(effect: containerEffect)
containerView.frame = button.bounds

containerView.userInteractionEnabled = false // Edit: so that subview simply passes the event through to the button

button.insertSubview(containerView, belowSubview: button.titleLabel!)
button.titleLabel?.font = UIFont.boldSystemFontOfSize(30)

let vibrancy = UIVibrancyEffect(forBlurEffect: containerEffect)
let vibrancyView = UIVisualEffectView(effect: vibrancy)
vibrancyView.frame = containerView.bounds
containerView.contentView.addSubview(vibrancyView)

vibrancyView.contentView.addSubview(button.titleLabel!)

And, here is what I have done,

  • Create a button.
  • Create a gradient layer and add it to 0 index of the button.
  • Create a container view with effect of UIBlurEffectDark.
  • Add the container view to the button.
  • Create a vibrancy view with the same effect and add it to the contentView of the above containerView.
  • Move the label from button to the vibrancy view's title

And, here is the final result. The titleLabel text blends nicely with vibrancy and the effect applied.

Sample Image

Add a blur effect to a UIButton

This can be done by creating the blur as you did and inserting it below the titleLabel of the UIButton. It's important to disable user interaction in order to allow the touches to not be intercepted by the the visual effect view and forwarded to the button.

Swift 4.2:

let blur = UIVisualEffectView(effect: UIBlurEffect(style:
UIBlurEffect.Style.light))
blur.frame = buttonForDisabling.bounds
blur.isUserInteractionEnabled = false //This allows touches to forward to the button.
buttonForDisabling.insertSubview(blur, at: 0)

Inserting a UIVisualEffectView behind an transparent imageView Image on a UIButton

After looking at the demo project, it looks like you need to add an imageView on top of the UIButton image. Here's my adjusted code - feel free to clean it up as needed!

private var imageView2 = UIImageView()  // declared globally

private func makeItFrosty() {
let frost = UIVisualEffectView(effect: UIBlurEffect(style: .light))
frost.frame = self.bounds
frost.autoresizingMask = [.flexibleWidth, .flexibleHeight]
if let imageView = imageView {
imageView.addSubview(frost)
// these three lines basically 'clone' the existing imageView and adds it on top of the 'frost' view
imageView2.image = imageView.image
imageView2.frame = imageView.frame
self.addSubview(imageView2)
}
}

I'm not too surprised that you need to do something like this - the UIButton image is likely deeply embedded to "bring to front".

button placed on top of UIVisualEffectView not responding

You need to add the target action (button.addTarget), i.e. what is the button supposed to do when pressed, see your code modified below:

    class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let image = UIImage(named: "face2")
let imageView = UIImageView(image: image)
imageView.frame = CGRectMake(0, 0, 200, 200)
let button = UIButton(frame: CGRectMake(50, 50, 100, 100))
button.setTitle("huhuhu", forState: UIControlState.Normal)
button.addTarget(self, action: "sayHi:", forControlEvents: .TouchUpInside)
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
button.userInteractionEnabled = true

let effect = UIBlurEffect(style: .Light)
let blurView = UIVisualEffectView(effect: effect)
blurView.frame = view.bounds

view.addSubview(imageView)
view.addSubview(blurView)
view.addSubview(button)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func sayHi(sender:UIButton) {
let randomIntH = arc4random_uniform(UInt32(view.bounds.size.width)-40)
let randomIntV = arc4random_uniform(UInt32(view.bounds.size.height)-15)
let frame = CGRectMake(CGFloat(randomIntH), CGFloat(randomIntV), 10, 10)
let label = UILabel(frame: frame)
label.text = "Hallo"
label.sizeToFit()
view.addSubview(label)
}
}

Adding a blur effect on a View Controller after segueing from it to another one modally

Blur transition does not come by default in iOS. So you need to customise code.

A simple solution would be to add a subview :

Add the following code while transitioning to vc2 :

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
blurEffectView = UIVisualEffectView(effect: blurEffect) // Global variable
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(blurEffectView)

NotificationCenter.default.addObserver(self, selector: #selector(self.removeBlurView), name: NSNotification.Name(rawValue: "removeBlurView"), object: nil)

Function for removing blur view (in vc1) :

func removeBlurView()
{
NotificationCenter.default.removeObserver(self)
blurEffectView.removeFromSuperview()
}

Add following in vc2 when you dismiss vc2 :

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "removeBlurView"), object: nil)

There are other ways to accomplish this as stated in Ray's tutorial and a closely related tutorial

Adding subview to a blurred view

I've subclassed UIVisualEffectView in code, and then added all kinds of controls as subviews to it. Very nice effect. Not sure if it can be done 100% through IB though due to view hierarchy expectations.

  • (1) Create the "main" view in IB and add constraints. The alternative here is what I did, subclass UIVisualEffectView.
  • (2) Create the "main" view subviews in code, add them to the "main" view, and add constraints in code.
  • (3) Since you want certain actions associated to some of the subviews (the UIButtons), also be sure to code addTarget(target:for:) calls for them.

ADDED EDIT: You may be able to do most of this through IB. Set everything up, and since the UIButtons are subviews to your blurred view, try using bringSubview(ToFront:). If that works, it's probably much easier! (I'm old-school and like coding things more.)



Related Topics



Leave a reply



Submit