UICollectionView标题在IBAction中改变高度
我想更改UICollectionView
中的标题的高度。 当我点击一个UIButton
。 做这个的最好方式是什么 ? 经过一些研究,我可以调用referenceSizeForHeaderInSection
但是我不知道如何在IBACtion
调用这个方法。 有任何想法吗 ? 谢谢你的帮助
你可以做的是检查你的button是否被点击,并基于你可以在你的referenceSizeForHeaderInSection
方法中设置不同的标题大小。 您不能显式调用referenceSizeForHeaderInSection
方法。 无论何时加载或重新绘制集合视图,都会调用此协议方法。
比方说,每当你点击button,你在你的IBAction方法中设置一个标志。 确保在类范围内声明它。
-(IBAction)buttonClicked{ self.flag = YES; // do whatever you need to do here //reload your collection view here [self.myCollectionView reloadData]; }
在委托方法中,您可以根据单击button的事实来设置标题大小。
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (!self.flag) { return CGSizeMake(0, kHeaderHeight); } else{ return CGSizeMake(0, kHeaderHeight + 50); } }
每当你想回到你正常的头大小,只需将该标志设置为NO,然后重新加载collectionView。
你想要做的方式是使委托返回一个不同的高度后,使布局无效。
-(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
试试这个stream程
// 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 }