Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView标题在iAction中更改高度_Ios_Objective C - Fatal编程技术网

Ios UICollectionView标题在iAction中更改高度

Ios UICollectionView标题在iAction中更改高度,ios,objective-c,Ios,Objective C,我想更改UICollectionView中标题的高度。当我点击ui按钮时。 最好的方法是什么? 经过一些研究,我可以调用referenceSizeForHeaderInstition,但我不知道如何在我的iAction中调用此方法。 有什么想法吗? 感谢您的帮助您希望这样做的方式是在布局无效后使代理返回不同的高度 -(IBAction) action { self.size = CGSizeMake(320,40); [self.collectionView.collectionlayou

我想更改
UICollectionView
中标题的高度。当我点击
ui按钮时
。 最好的方法是什么? 经过一些研究,我可以调用
referenceSizeForHeaderInstition
,但我不知道如何在我的
iAction
中调用此方法。 有什么想法吗?
感谢您的帮助

您希望这样做的方式是在布局无效后使代理返回不同的高度

-(IBAction) action {

 self.size = CGSizeMake(320,40);
 [self.collectionView.collectionlayout invalidateLayout];
}
在您的委托中,它应该如下所示:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == theSection)
        return self.size;

 return CGSizeZero;
}

另外,请确保可重用视图的大小正确。

要执行此操作,请在布局无效后使代理返回不同的高度

-(IBAction) action {

 self.size = CGSizeMake(320,40);
 [self.collectionView.collectionlayout invalidateLayout];
}
在您的委托中,它应该如下所示:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == theSection)
        return self.size;

 return CGSizeZero;
}

还要确保可重用视图的大小正确。

您可以做的是检查按钮是否被单击,并基于此,您可以在
ReferenceSizeForHeaderInstruction
方法中设置不同的标题大小。不能显式调用
referenceSizeForHeaderInstition
方法。无论何时加载或重新绘制集合视图,都会调用此协议方法

比方说,每当单击按钮时,都会在iAction方法中设置一个标志。确保在类范围内声明它

-(IBAction)buttonClicked{
    self.flag = YES;
    // do whatever you need to do here

    //reload your collection view here
    [self.myCollectionView reloadData];
}
在委托方法中,根据单击按钮的情况设置标题大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  {
  if (!self.flag) {
     return CGSizeMake(0, kHeaderHeight);
  }
  else{
    return CGSizeMake(0, kHeaderHeight + 50);
  } 
}

无论何时,只要将标志设置为“否”并重新加载collectionView,您就可以返回到正常的页眉大小。

您可以检查按钮是否被单击,并基于此,您可以在
ReferenceSizeForHeaderInstitution
方法中以不同的方式设置页眉大小。不能显式调用
referenceSizeForHeaderInstition
方法。无论何时加载或重新绘制集合视图,都会调用此协议方法

比方说,每当单击按钮时,都会在iAction方法中设置一个标志。确保在类范围内声明它

-(IBAction)buttonClicked{
    self.flag = YES;
    // do whatever you need to do here

    //reload your collection view here
    [self.myCollectionView reloadData];
}
在委托方法中,根据单击按钮的情况设置标题大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  {
  if (!self.flag) {
     return CGSizeMake(0, kHeaderHeight);
  }
  else{
    return CGSizeMake(0, kHeaderHeight + 50);
  } 
}

每当您想返回到正常的页眉大小时,只需将标志设置为“否”,然后再次重新加载collectionView。

您可以在
iAction中尝试此流程。

    // Create a new UICollectionViewFlowLayout with new values

    let cFlowTest = UICollectionViewFlowLayout()
    cFlowTest.itemSize = CGSizeMake(40, 40) // your default collection cell size
    cFlowTest.minimumInteritemSpacing = 44 // spacing
    cFlowTest.minimumLineSpacing = 44 // spacing
    cFlowTest.estimatedItemSize = CGSizeMake(40, 40) // if you'll use auto - sizing set this property and don't set 'itemSize' and make sure your constraint supported this
    cFlowTest.scrollDirection = .Vertical // or .Horizontal
    cFlowTest.headerReferenceSize = CGSizeMake(300, 40) // set your new header size
    // cFlowTest.footerReferenceSize = CGSizeMake(300, 40) // set your new footer size if you have

    // Then update your collectionView's flowlayout using this two methods
    // 1.
    collectionView.setCollectionViewLayout(cFlowTest, animated: true)

    // or
    // 2.
    collectionView.setCollectionViewLayout(cFlowTest, animated: true) { (isFinished) -> Void in
        // your additional code here
    }

您可以在
iAction

    // Create a new UICollectionViewFlowLayout with new values

    let cFlowTest = UICollectionViewFlowLayout()
    cFlowTest.itemSize = CGSizeMake(40, 40) // your default collection cell size
    cFlowTest.minimumInteritemSpacing = 44 // spacing
    cFlowTest.minimumLineSpacing = 44 // spacing
    cFlowTest.estimatedItemSize = CGSizeMake(40, 40) // if you'll use auto - sizing set this property and don't set 'itemSize' and make sure your constraint supported this
    cFlowTest.scrollDirection = .Vertical // or .Horizontal
    cFlowTest.headerReferenceSize = CGSizeMake(300, 40) // set your new header size
    // cFlowTest.footerReferenceSize = CGSizeMake(300, 40) // set your new footer size if you have

    // Then update your collectionView's flowlayout using this two methods
    // 1.
    collectionView.setCollectionViewLayout(cFlowTest, animated: true)

    // or
    // 2.
    collectionView.setCollectionViewLayout(cFlowTest, animated: true) { (isFinished) -> Void in
        // your additional code here
    }
Swift 5:

self.collectionView.collectionViewLayout.invalidateLayout()
Swift 5:

self.collectionView.collectionViewLayout.invalidateLayout()

感谢您的帮助,当我在collectionView上重新加载数据时,会再次调用ReferenceSizeForHeaderInstruction。你认为我可以在标题向下滚动时添加动画吗?是的,你可以。UIView动画宽度的研究。但是,要使标题展开,您可能需要使用标题的帧。调用重新加载数据实际上是一种浪费,因为数据没有改变。在这种情况下,您应该调用
invalidateLayout
注意我下面的回答,它也更容易进行动画。这正是我所做的,但我也喜欢添加动画。您成功使其工作了吗?感谢您的帮助,当我在collectionView上重新加载数据时,ReferenceSizeForHeaderInstitution再次被调用。你认为我可以在标题向下滚动时添加动画吗?是的,你可以。UIView动画宽度的研究。但是,要使标题展开,您可能需要使用标题的帧。调用重新加载数据实际上是一种浪费,因为数据没有改变。在这种情况下,您应该调用
invalidateLayout
注意我下面的回答,它也更容易进行动画。这正是我所做的,但我也喜欢添加动画。你设法让它工作了吗?