سؤال

it appears that viewDidLayoutSubviews is called immediately after layoutSubviews is called on a view, before layoutSubviews is called on the subviews of that view. Is there any way of knowing when layoutSubviews has been called on a view and all of its children that also needed their layouts updated?

هل كانت مفيدة؟

المحلول

You shouldn't have to know if the subviews of a subview have updated their layout: That sounds like too tight coupling. Also, each subview might handle the arrangement of their respective subviews differently and might not (need to) call layoutSubviews for its subviews at all. You should only ever have to know about your direct subviews. You should treat them more or less as black boxes and not care whether they have subviews of their own or not.

نصائح أخرى

As @Johannes Fahrenkrug said, you should "treat them as black boxes". But according to my understandings, it is because that Cocoa just can't promise it.

If you really need to be notified when all subviews have done the layout job, here is a hardcore sample may solve your problem. I don't either promise it would work under every 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's 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++;
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top