2017-09-22 4 views
3

J'ai une fonction ci-dessous et quand je créer un lien avec le SDK iOS 11, je reçois une erreur:Avertissement de iOS « Ne pas ajouter de sous-vues directement à la vue de l'effet visuel lui-même »

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

Le problème peut être résolu en changeant

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

à

effectView = UIView() 

mais l'effet est pas présent de cette façon. Comment puis-je continuer à utiliser UIVisualEffectView au lieu de UIView? Je veux garder l'effet.

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) 
} 

Répondre

6

Il suffit de suivre ce que l'erreur dit et ajoutez vos subviews à l » contentViewUIVisualEffectView.

effectView.contentView.addSubview(activityIndicator) 
effectView.contentView.addSubview(strLabel) 
0

Apple dit que;

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.

Ajouter votre subviews pour afficher le contenu de vue de l'effet visuel

effectView.contentView.addSubview(activityIndicator) 
effectView.contentView.addSubview(strLabel) 
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) 
} 

au moment du départ:

 activityIndicator("Your Text") 

lorsque vous avez terminé:

self.effectView.removeFromSuperview()