IOS: determine when all child objects had a layout

it seems that viewDidLayoutSubviews is called immediately after calling layoutSubviews in the view, before layoutSubviews is called in the subzones of this view. Is there a way to find out when layoutSubviews was called in a view and all its children also needed to update their layouts?

+8
ios objective-c uiviewcontroller uiview layoutsubviews
source share
2 answers

You do not need to know if the subview subheadings have updated their layout: this seems like a too tight connection. In addition, each subview may handle the layout of its respective subzones differently and may (not need to) call layoutSubviews for its subroutines in general. You only need to know about your direct peeks. You should treat them more or less like black boxes without worrying about whether they have their own approaches or not.

+1
source share

As @Johannes Fahrenkrug said, you should "treat them like black boxes." But, in my understanding, this is because Cocoa simply cannot promise.

If you really need to be notified when all the subheadings have completed the layout task, here is an example of a hardcore can solve your problem. I do not promise that this will work in any situation.

 - (void) layoutSubviewsIsDone{ // Your code here for layoutSubviews is done } // Prepare two parameters ahead int timesOfLayoutSubviews = 0; BOOL isLayingOutSubviews = NO; // Override the layoutSubviews function - (void) layoutSubviews{ isLayingOutSubviews = YES; // It unsafe here! // you may move it to appropriate place according to your real scenario // Don't forget to inform super [super layoutSubviews]; } // Override the setFrame function to monitor actions of layoutSubviews - (void) setFrame:(CGRect)frame{ if(isLayingOutSubviews){ if(frame.size.width == self.frame.size.width && frame.size.height == self.frame.size.height && frame.origin.x == self.frame.origin.x && frame.origin.y == self.frame.origin.y && timesOfLayoutSubviews ==self.subviews.count){ isLayingOutSubviews = NO; timesOfLayoutSubviews = 0; [self layoutSubviewsIsDone]; // Detected job done, call your function }else{ timesOfLayoutSubviews++; } } 
0
source share

All Articles