How add subview to UIView in Swift 4? Need work in Swift 3 and Swift 4 (iOS 10+)
@IBOutlet weak var viewBody: UIView!
@objc var loadIndicator = UIActivityIndicatorView()
@objc let loadBody = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
@objc func activityLoad() {
DispatchQueue.main.async() {
self.loadIndicator.removeFromSuperview()
self.loadBody.removeFromSuperview()
self.loadBody.frame = CGRect(x: self.viewBody.frame.midX - 23, y: self.viewBody.frame.midY - 23, width: 46, height: 46)
self.loadBody.layer.cornerRadius = 10
self.loadBody.alpha = 1//0.7
self.loadBody.layer.masksToBounds = true
self.loadIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
self.loadIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46)
self.loadIndicator.startAnimating()
self.loadBody.addSubview(self.loadIndicator)
self.viewBody.addSubview(self.loadBody)
}
}
Error: Do not add subviews directly to the visual effect view itself, instead add them to the -contentView.'
if i add self.viewBody.contentView.addSubview(self.loadBody)
Error: Value of type UIView has no member contentView.
I need your help. Thank you so much.
UIVisualEffectView has a contentView property, and you should add subview to this contentView. So just change your code to self.loadBody.contentView.addSubview(self.loadIndicator). – mrfour 20 mins ago
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 .
Add your subviews to content view of visual effect view
self.loadBody.contentView.addSubview(self.loadIndicator)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.