简体   繁体   中英

How to add constraints for UIImage in dynamic cell only after auto layout has drawn the view

I'm trying to draw a circular image in a tableview cell. I have figured out how to draw the circle when the image is in a normal view: I place this code in viewDidLayoutSubviews () so as to apply my changes to the image only after autolayout has done its thing.

 profilePicture2.layer.cornerRadius = profilePicture2.frame.size.height/2
    profilePicture2.clipsToBounds = true

I got that tip from this answer: Why does adding an align with centre constraint to a UIImage throw out the radius of corners setting?

But how do I do it if the image is in a dynamic tableview cell? Is there a way to use viewDidLayoutSubviews() for a single cell, so I can set the corner radius of my image AFTER autolayout has done its thing?

Thanks in advance.

You can create a custom class for profilePicture which extends UIView and override layoutSubviews .

override func layoutSubviews() {
    layer.cornerRadius = frame.size.height/2
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM