当我在SwiftUI中创建视图并在Xcode预览中使用渲染时PreviewLayout.sizeThatFits,预览会根据其内容调整其大小。当我使用导入UIKIt视图时UIViewRepresentable,它会显示完整的设备尺寸框架。
有没有一种方法,使SwiftUI尊重intrinsicContentSize的UIView子类?
struct Label: UIViewRepresentable {
func makeUIView(context: UIViewRepresentableContext<Label>) -> UILabel {
return UILabel()
}
func updateUIView(_ uiView: UILabel, context: UIViewRepresentableContext<Label>) {
uiView.text = "Some text"
}
}
#if DEBUG
struct Label_Previews: PreviewProvider {
static var previews: some View {
Group {
Label().previewLayout(.sizeThatFits)
}
}
}
#endif
Run Code Online (Sandbox Code Playgroud) 我有一个UICollectionView可以添加和删除的单元格.我正在使用这些更改performBatchUpdates,并且布局按预期动画.
当我滚动到内容的末尾并删除一个项目以使contentSize减少时出现问题:这会导致contentOffset更改,但更改不是动画.相反,contentOffset删除动画完成后立即跳转.我已经尝试手动更新contentOffset删除旁边,但这对我也不起作用.
我正在使用自定义布局,但我使用以下代码看到标准流布局的相同行为:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.items.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [self.items objectAtIndex:indexPath.item];
return cell;
}
- (IBAction)addItem:(UIBarButtonItem *)sender
{
self.runningCount++;
[self.items addObject:[NSString stringWithFormat:@"Item %u",self.runningCount]];
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.items.count-1 inSection:0]]];
} completion:nil];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{ …Run Code Online (Sandbox Code Playgroud)