Warning from iOS "Do not add subviews directly to the visual effect view itself"
Asked Answered
W

3

8

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

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

The problem can be solved by changing

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

to

effectView = UIView()

but the effect is not present that way. How can I keep using UIVisualEffectView instead of 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)
}
Wits answered 22/9, 2017 at 2:20 Comment(0)
C
12

Just follow what the error says and add your subviews to UIVisualEffectView's contentView.

effectView.contentView.addSubview(activityIndicator)
effectView.contentView.addSubview(strLabel)
Collen answered 22/9, 2017 at 21:0 Comment(0)
C
6

Apple says that;

Depending on the desired effect, the effect may affect content layered behind the view or content added to the visual effect view’s contentView. Apply a visual effect view to an existing view and then apply a UIBlurEffect or UIVibrancyEffect object to apply a blur or vibrancy effect to the existing view. After you add the visual effect view to the view hierarchy, add any subviews to the contentView property of the visual effect view. Do not add subviews directly to the visual effect view itself.

So, as per @the4kman add your subviews to content view of visual effect view

 effectView.contentView.addSubview(activityIndicator)
 effectView.contentView.addSubview(strLabel)
Crazyweed answered 25/11, 2017 at 12:3 Comment(0)
P
0
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)
}

when start :

     activityIndicator("Your Text")

when Finished :

    self.effectView.removeFromSuperview()
Proletarian answered 16/1, 2018 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.