- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:kContentSizeKey])
{
CGSize contentSize;
id newValue = [change valueForKey:NSKeyValueChangeNewKey];
[(NSValue*)newValue getValue:&contentSize];
self.tableView.height = contentSize.height;
[self.collectionView reloadData];
YDLog(@"🤣contentSize=%@,string=%@",newValue, NSStringFromCGSize(contentSize));
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
输出:
🤣contentSize=NSSize: {414, 1002.57678125},string={414, 1002.57678125}
注意:newValue并不等于NSStringFromCGSize(contentSize),所以不能用key直接取值,因为它存的不是一个字符串,而是一个NSSize结构体
网友评论