UITableView/UIScrollView how to automatically know when ContentSize changes? UITableView/UIScrollView how to automatically know when ContentSize changes? ios ios

UITableView/UIScrollView how to automatically know when ContentSize changes?


The answer is actually simple using KVO (Key Value Observing);

- (id)initWithFrame:(CGRect)frame tableView:(UITableView *)tableView{    // ....    [self.tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionPrior context:NULL];    // ....}- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    if ([keyPath isEqualToString:@"contentSize"])     {        // Do something    }}

I am not sure about the flags yet though.


For those who don't like KVO:

- (void)setContentSize:(CGSize)contentSize {    [super setContentSize:contentSize];    // Do something.}

Yeah, just use super to access the parent method. This assume you subclass UIScrollview/TableView/CollectionView