IOS warning "Do not add subviews directly to the visual effect view"

I have a function below, and when I contact the iOS 11 SDK, I get an error:

Do not add subviews directly to the visual effect view, but add them to -contentView.

The problem can be solved by changing

let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))"

to

effectView = UIView()

but there is no effect. How can i use UIVisualEffectViewinstead UIView? I want to keep the effect.

let imagePicker = UIImagePickerController()
let messageFrame = UIView()
var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()    
let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))

func activityIndicator(_ title: String) {  
    strLabel.removeFromSuperview()
    activityIndicator.removeFromSuperview()
    effectView.removeFromSuperview()

    strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 160, height: 46))
    strLabel.text = title
    strLabel.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
    strLabel.textColor = UIColor(white: 0.9, alpha: 0.7)

    effectView.frame = CGRect(x: (view.frame.midX - strLabel.frame.width/2), y: (view.frame.midY - strLabel.frame.height/2), width: 160, height: 46)
    effectView.layer.cornerRadius = 15
    effectView.layer.masksToBounds = true

    activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
    activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46)
    activityIndicator.startAnimating()

    effectView.addSubview(activityIndicator)
    effectView.addSubview(strLabel)
    view.addSubview(effectView)
}
+4
source share
3 answers

Just follow what the error says and add your routines to UIVisualEffectView contentView.

effectView.contentView.addSubview(activityIndicator)
effectView.contentView.addSubview(strLabel)
+7
source

Apple says that:

, contentView. , UIBlurEffect UIVibrancyEffect, . , contentView . subviews .

 effectView.contentView.addSubview(activityIndicator)
 effectView.contentView.addSubview(strLabel)
+1
let messageFrame = UIView()
var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()
effectView = UIView()

   func activityIndicator(_ title: String) {

    strLabel.removeFromSuperview()
    activityIndicator.removeFromSuperview()
    effectView.removeFromSuperview()

    strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 160, height: 46))
    strLabel.text = title
    strLabel.font = UIFont.systemFont(ofSize: 14, weight: UIFontWeightMedium)
    strLabel.textColor = UIColor(white: 0.9, alpha: 0.7)

    effectView.frame = CGRect(x: view.frame.midX - strLabel.frame.width/2, y: view.frame.midY - strLabel.frame.height/2 , width: 160, height: 46)
    effectView.layer.cornerRadius = 15
    effectView.layer.masksToBounds = true

    activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
    activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46)
    activityIndicator.startAnimating()

    effectView.addSubview(activityIndicator)
    effectView.addSubview(strLabel)
    view.addSubview(effectView)
}

:

     activityIndicator("Your Text")

:

    self.effectView.removeFromSuperview()
0

Source: https://habr.com/ru/post/1686170/


All Articles