2017-09-22 33 views
3

我有以下功能和警告,当我跟iOS 11 SDK链接,我得到一个错误:从iOS版“不要直接子视图添加到视觉效果视图本身”

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

问题就可以通过改变

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

解决了

effectView = UIView() 

但效果不存在这样的。我如何继续使用UIVisualEffectView而不是UIView?我想保持效果。

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

回答

6

只需按照错误说什么,并添加子视图UIVisualEffectViewcontentView

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

苹果说,

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.

添加您的子视图的视觉效果视图的内容视图

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

时开始:

 activityIndicator("Your Text") 

完成后:

self.effectView.removeFromSuperview() 
相关问题