我正在尝试使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
答案 0 :(得分:0)
我找到了一个解决方案,您可以将视图放置在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())