【问题标题】:Uitextfield background color blurry?Uitextfield背景颜色模糊?
【发布时间】:2019-02-21 23:42:40
【问题描述】:

我正在尝试使 UItextfield 的背景颜色变得模糊。当我尝试下面的代码时,我的应用程序在运行时崩溃。有没有人尝试过这个并且知道如何使 UITextfield 模糊?

    let p = UITextField()
    let blurEffect = UIBlurEffect(style: .light)
    let blurView = UIVisualEffectView(effect: blurEffect)
    p.layer.isOpaque = true
    p.layer.backgroundColor = blurView as! CGColor

【问题讨论】:

    标签: xcode uitextfield background-color uiblureffect


    【解决方案1】:

    我找到了一个解决方案,您可以在 UITextfield 后面放置一个视图,并使其透明。

    let v = UIView()
    v.frame = CGRect(x: 30, y: 100, width: 180, height: 30)
    let blurEffect = UIBlurEffect(style: .light)
    let blurView = UIVisualEffectView(effect: blurEffect)
    blurView.frame = v.bounds
    blurView.backgroundColor = .clear
    v.addSubview(blurView)
    
    let p = UITextField()
    p.frame = CGRect(x: 0, y: 0, width: 180, height: 30)
    p.layer.isOpaque = true
    p.backgroundColor = .clear
    v.addSubview(p)
    
    self.view.backgroundColor = .red
    self.view.addSubview(v)
    

    这是example 提出的解决方案,用背景图片代替红色,以强调模糊效果

    self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background") ?? UIImage())
    

    【讨论】:

    • 感谢您的回复。我曾尝试使用您的解决方案,但由于某种原因,它对我不起作用。视图根本不显示。
    • 哪个视图没有显示?在我的代码中,我假设您在视图控制器中工作。我还为帧 CGRect 使用了固定值,但您可以使用自动布局放置它。
    • @Brian 看看样本并告诉我
    • 如果图像不断变化,这会起作用吗?说模糊视图是在用户提交的照片上?模糊的背景会显示不同的照片吗?
    • @Brian 当然是的。模糊只是一种视觉效果,它取决于应用视图背后的内容。
    猜你喜欢
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2018-04-20
    相关资源
    最近更新 更多