Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Can a UIView know that the layout process for it has completed?

Tags:

ios

When using auto layout, the view's size is unknown when it is initialised, this brings a problem to me.

When using UIImageView, I wrote a category that can load image from my own CDN by setting the image URL to UIImageView, my CDN stores one image with different sizes so that difference devices can load the size it really needs.

I want to make my UIImageView be able to load the URL for the resolution it needs, but when my UIImageView get the URL, the size of it is not yet determined by auto layout.

So is there a way for UIView to know that the layout process for it has finished for the first time?

like image 855
CarmeloS Avatar asked Dec 21 '25 18:12

CarmeloS


1 Answers

there is a method for UIView.You could override it.

-(void)layoutSubviews {
    CGRect bounds =self.bounds;
    //build your imageView's frame here
    self.imageView=imageViewFrame.
}

In Swift 5.X

override func layoutSubviews() {
    super.layoutSubviews()
    let myFrame = = self.bounds
}

If you have other complex items in the custom view, don't forget to call super if you override layoutSubviews()...

like image 65
Destiny Avatar answered Dec 23 '25 06:12

Destiny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!