【问题标题】:Why does not it show content with the right size directly?为什么不直接显示大小合适的内容?
【发布时间】:2020-12-26 00:47:58
【问题描述】:

我想单击按钮然后显示警报视图。此警报视图将接收两个数组。

警报视图将通过两个 UICollectionView 布局大小自行刷新。

[self.topCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:self.topSelectIndex inSection:0] animated:false scrollPosition:UICollectionViewScrollPositionBottom];
     
[self.bottomCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:self.bottomSelectIndex inSection:0] animated:false scrollPosition:UICollectionViewScrollPositionBottom];


 [self layoutIfNeeded];

  CGFloat topFloatHeight = self.topCollectionView.collectionViewLayout.collectionViewContentSize.height;
  CGFloat bottomFloatHeight = self.bottomCollectionView.collectionViewLayout.collectionViewContentSize.height;

   self.topCollectionHeightConstrait.constant = topFloatHeight;
    self.bottomCollectionHeightConstraint.constant = bottomFloatHeight;

//    CGFloat bigScrollViewHeight = self.bigScrollView.contentLayoutGuide.heightAnchor
    CGFloat catHeight = topFloatHeight + bottomFloatHeight + 200;
    CGFloat screenHeight = kScreenHeight - 180;
    if( catHeight < screenHeight) {
        self.scrollBoxHeightCont.constant = catHeight;
    }else {
        self.scrollBoxHeightCont.constant = screenHeight;
    }

   self.bigScrollView.contentSize = CGSizeMake(0, catHeight);

 [self.topCollectionView reloadData];
 
 [self.bottomCollectionView reloadData];

如您所见,关键代码是 self.bigScrollView.contentSize = CGSizeMake(0, catHeight);

代码改变了 bigScrollview 的 contentSize,alert view 下次总是显示上面代码内容中计算的正确大小,而不是这次。

为什么不直接显示大小合适的内容? 是否有其他解决方案可以获得相同的结果?

【问题讨论】:

    标签: ios uicollectionview uiscrollview


    【解决方案1】:

    在集合视图重新加载新数据后。 在客户视图展示前,添加两行代码:

    [自设需求布局];
    [自我布局IfNeeded];

    将计算高度代码移动到layoutSubviews 方法中。

    - (void)layoutSubviews {
    [super layoutSubviews];
    
    
      CGFloat topFloatHeight = self.topCollectionView.collectionViewLayout.collectionViewContentSize.height;
      CGFloat bottomFloatHeight = self.bottomCollectionView.collectionViewLayout.collectionViewContentSize.height;
    
       self.topCollectionHeightConstrait.constant = topFloatHeight;
        self.bottomCollectionHeightConstraint.constant = bottomFloatHeight;
    
    //    CGFloat bigScrollViewHeight = self.bigScrollView.contentLayoutGuide.heightAnchor
        CGFloat catHeight = topFloatHeight + bottomFloatHeight + 200;
        CGFloat screenHeight = kScreenHeight - 180;
        if( catHeight < screenHeight) {
            self.scrollBoxHeightCont.constant = catHeight;
        }else {
            self.scrollBoxHeightCont.constant = screenHeight;
        }
    
    
     self.bigScrollView.contentSize = CGSizeMake(0, catHeight);
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多