I have created a custom view class. I have a image view that I want to blur. I have added the blurring effect in the code below, but obviously it doesn't work because the bounds of the image view is wrong. Due to auto layout haven't calculated the correct sizes for height and width of the image view yet. However which method should I override to access correct position and size attributes?
And what would be the solution for only adding it once?
class ProductListingCell: UICollectionViewCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var sharpImageView: UIImageView!
@IBOutlet weak var blurImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
let blurEffect = UIBlurEffect(style: .Light)
let visualEffectView = UIVisualEffectView(effect: blurEffect)
visualEffectView.frame = blurImageView.bounds
blurImageView.addSubview(visualEffectView)
}
}
(edit) Maybe an answer:
Just after posting the question I found this method. Would this be the correct way to do it?
override func layoutSubviews() {
super.layoutSubviews()
layoutIfNeeded()
let blurEffect = UIBlurEffect(style: .Light)
let visualEffectView = UIVisualEffectView(effect: blurEffect)
visualEffectView.frame = blurImageView.bounds
blurImageView.addSubview(visualEffectView)
}