Blur effect - Background UITextField

风格不统一 提交于 2019-12-06 15:04:09

问题


I use Swift 2 and Xcode 7.

I would like to blur in the background of my UITextField. I have not found a property backgroundView. Only background (for background image) or backgroundColor.

I would have come to add a view in background. But I do not know how to make a UITextField.

Do you have a solution ?


回答1:


You can only add some kind of view to make it a blur view. Now the problem is that textfields don't have a backgroundView property but they do have a background property where we can set an UIImage. So we can make a UIImage from a UIView too

This method will create a UIImage for the UIView passed.

func imageWithView(view:UIView)->UIImage{
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
        view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

Now we will use a UIBlurEffect and UIVisualEffectView which are available for iOS 8+

//only apply the blur if the user hasn't disabled transparency effects
if !UIAccessibilityIsReduceTransparencyEnabled() {
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) 
    //you can try for more values like .ExtraLight and .Dark

    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = textField.bounds
    blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    textField.backgroundColor = UIColor.clearColor()

    //Set the blurred image made from blurred view as textfield's background
    textField.background = imageWithView(blurEffectView)
}

For Swift 3.0

func imageWithView(view:UIView)->UIImage {
       UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
       view.layer.render(in: UIGraphicsGetCurrentContext()!)
       let image = UIGraphicsGetImageFromCurrentImageContext()
       UIGraphicsEndImageContext()
       return image!
}

if !UIAccessibilityIsReduceTransparencyEnabled() {
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
    //you can try for more values like .extraLight and .dark

    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = textField.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    textField.backgroundColor = UIColor.clear

    //Set the blurred image made from blurred view as textfield's background
    textField.background = imageWithView(view: blurEffectView)
}

Here is the screenshot attached

Hope this helps




回答2:


Do you have a solution?

You can always put a transparent text field on top of another view. If that view happens to have the same size as the text field, it'll look just like the background view of the text field. You could even make the text field a subview of the "background" view, so that you can move them around together more easily.




回答3:


You can set the background color to white, and change its alpha value:

myTextField.alpha = 0.5;

Probably not exactly what you asked, but much easier to implement.




回答4:


Swift 5, Xcode 10:

I did this by subclassing the UIView class, and added a blur effect and a text field to it:

import UIKit

class BlurryTextField: UIView {

    private lazy var blurView: UIVisualEffectView = {
        let blurEffect = UIBlurEffect(style: .light)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.translatesAutoresizingMaskIntoConstraints = false
        blurView.clipsToBounds = true
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        return blurView
    }()

    lazy var textField: UITextField = {
        let view = UITextField()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .clear
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.cornerRadius = 10
        self.clipsToBounds = true
        self.backgroundColor = .clear
        setupConstraints()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    private func setupConstraints() {
        self.addSubview(blurView)
        self.addSubview(textField)

        blurView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive     = true
        blurView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive   = true
        blurView.topAnchor.constraint(equalTo: self.topAnchor).isActive             = true
        blurView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive       = true

        textField.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive    = true
        textField.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive  = true
        textField.topAnchor.constraint(equalTo: self.topAnchor).isActive            = true
        textField.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive      = true
    }
}

And then in your UIViewController:

// Create an instance of BlurryTextField
private lazy var blurryTF: BlurryTextField = {
    let view = BlurryTextField()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

override func viewDidLoad() {
    super.viewDidLoad()

    // Add the instance to the top of our view
    view.addSubview(blurryTF)
    blurryTF.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    blurryTF.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    blurryTF.heightAnchor.constraint(equalToConstant: 55).isActive = true
}

Here's a preview of my approach:



来源:https://stackoverflow.com/questions/34188042/blur-effect-background-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!