iOS: HorizontalList support fit width or height

This commit is contained in:
pengfei.zhou 2022-08-25 13:24:16 +08:00 committed by osborn
parent 921829ba53
commit 0c868f095d

View File

@ -39,6 +39,18 @@ @interface DoricHorizontalTableView : UICollectionView
@implementation DoricHorizontalTableView @implementation DoricHorizontalTableView
- (CGSize)sizeThatFits:(CGSize)size { - (CGSize)sizeThatFits:(CGSize)size {
if (self.doricLayout.widthSpec == DoricLayoutFit
|| self.doricLayout.heightSpec == DoricLayoutFit) {
CGFloat width = size.width;
CGFloat height = size.height;
if (self.doricLayout.widthSpec == DoricLayoutFit && self.contentSize.width > 0) {
width = self.contentSize.width;
}
if (self.doricLayout.heightSpec == DoricLayoutFit && self.contentSize.height > 0) {
height = self.contentSize.height;
}
return CGSizeMake(width, height);
}
return [super sizeThatFits:size]; return [super sizeThatFits:size];
} }
@end @end
@ -200,6 +212,14 @@ - (void)blend:(NSDictionary *)props {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
self.rowCount = self.itemCount + (self.loadMore ? 1 : 0); self.rowCount = self.itemCount + (self.loadMore ? 1 : 0);
[self.view reloadData]; [self.view reloadData];
if ((self.view.width == 0 && self.view.doricLayout.widthSpec == DoricLayoutFit)
|| (self.view.height == 0 && self.view.doricLayout.heightSpec == DoricLayoutFit)) {
DoricSuperNode *node = self.superNode;
while (node.superNode != nil) {
node = node.superNode;
}
[node requestLayout];
}
}); });
} }
self.needReload = false; self.needReload = false;
@ -362,12 +382,27 @@ - (void)callItem:(NSUInteger)position width:(CGFloat)width {
self.itemWidths[@(position)] = @(width); self.itemWidths[@(position)] = @(width);
if (@available(iOS 12.0, *)) { if (@available(iOS 12.0, *)) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
if (self.view.doricLayout.heightSpec == DoricLayoutFit) { if (self.view.doricLayout.widthSpec == DoricLayoutFit
|| self.view.doricLayout.heightSpec == DoricLayoutFit) {
DoricSuperNode *node = self.superNode; DoricSuperNode *node = self.superNode;
while (node.superNode != nil) { while (node.superNode != nil) {
node = node.superNode; node = node.superNode;
} }
[node requestLayout]; [node requestLayout];
if (self.view.doricLayout.heightSpec == DoricLayoutFit) {
CGFloat height = 0;
for (UICollectionViewCell *tableViewCell in self.view.visibleCells) {
if ([tableViewCell isKindOfClass:[DoricHorizontalTableViewCell class]]) {
DoricHorizontalListItemNode *node = ((DoricHorizontalTableViewCell *) tableViewCell)
.doricHorizontalListItemNode;
height = MAX(height, node.view.height);
}
}
self.view.height = height;
}
if (self.view.doricLayout.widthSpec == DoricLayoutFit) {
self.view.width = self.view.contentSize.width;
}
} }
[UIView performWithoutAnimation:^{ [UIView performWithoutAnimation:^{
NSUInteger itemCount = self.rowCount; NSUInteger itemCount = self.rowCount;