我试图在我的视图中添加模糊效果。我像这样创建了模糊效果
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
return blurEffectView
}()
然后我像这样将它添加到子视图中,并使用它设置约束
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
let view = tableViewCell
addSubview(view)
view.addSubview(blurLabel)
blurLabel.leftAnchor.constraint(equalTo: self.leftAnchor,constant:0).isActive = true
//blurLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 178).isActive = true
blurLabel.widthAnchor.constraint(equalToConstant: 375).isActive = true
blurLabel.heightAnchor.constraint(equalToConstant: 180).isActive = true
但是当我运行应用程序时,它并没有出现在
中答案 0 :(得分:0)
编辑:正如@HAS& amp; @Fogmeister,您也可以使用translatesAutoResizingMaskIntoConstraints = false
,因为它是使用自动布局而不指定显式帧的更好解决方案。
您必须将框架分配给UIVisualEffectView
,如下所示:
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = CGRect(x: 0, y: 0, width: 60, height: 40) //give desirable frame
return blurEffectView
}()
答案 1 :(得分:0)
如果您愿意,您还可以将“blurView”提供给任何视图或子视图,而不是提供明确的帧。
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = myMainview.bounds //Blur effect's frame
return blurEffectView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Use it somewhere using
self.view.insertSubview(blurEffectView, belowSubview: myAnotherView)
// You may also use any of the subviews too, instead of the self.view
/// self.myView.insertSubview(blurEffectView, belowSubview: myAnotherView)
}
答案 2 :(得分:0)
要解决此问题,您只需停止自动创建约束即可。像这样......
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
// this line will stop constraints being added for a zero frame
blurEffectView.translatesAutoResizingMaskIntoConstraints = false
return blurEffectView
}()