模糊效果 - 背景UITextField [英] Blur effect - Background UITextField

查看:211
本文介绍了模糊效果 - 背景UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Swift 2和Xcode 7.

I use Swift 2 and Xcode 7.

我想在我的UITextField的背景中模糊。我还没有找到属性 backgroundView 。只有背景(用于背景图片)或 backgroundColor

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.

我本来会在后台添加一个视图。但我不知道如何制作UITextField。

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

你有解决方案吗?

推荐答案

您只能添加某种视图才能使其成为模糊视图。现在的问题是textfields没有backgroundView属性,但是它们有一个background属性,我们可以在其中设置 UIImage 。所以我们可以从 UIView 创建一个 UIImage

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

此方法将为传递的UIView创建一个UIImage。

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
    }

现在我们将使用 UIBlurEffect UIVisualEffectView 适用于iOS 8 +

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




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

以下是附件截图

希望这会有所帮助

这篇关于模糊效果 - 背景UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆