2017-09-22 34 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() 
相關問題