2012-10-22 291 views
8

我试图调整UICollectionView的高度,将视图控制器加载时设置为0,然后在按下按钮时使用动画增加其大小。我尝试了一些不同的东西,但它的大小根本不变。这里的所有不同的东西,我试图把它的高度更改为0:调整大小UICollectionView高度

CGRect bounds = [self.collectionView bounds]; 
[self.collectionView setBounds:CGRectMake(bounds.origin.x, 
              bounds.origin.y, 
              bounds.size.width, 
              bounds.size.height - 100)]; 

....

CGRect frame = [self.collectionView frame]; 
[self.collectionView setFrame:CGRectMake(frame.origin.x, 
           frame.origin.y, 
           frame.size.width, 
           frame.size.height - 100)]; 

....

CGRect frame = self.collectionView.frame; 
frame.size.height -= 100; 
self.collectionView.frame = frame; 

....

self.collectionView.clipsToBounds = YES; 
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
+0

是'self.collectionView'实际设置的东西?你在使用AutoLayout吗? – mattjgalloway

+0

我将集合视图设置为出口。是AutoLayout已打开。 –

+5

如果你使用AutoLayout,那么你将不得不改变约束而不是直接改变框架。您需要在集合视图上设置高度限制。 – mattjgalloway

回答

4

如果您使用Interface Builder for UICollectionView i在创建CollectionView的xib文件中,从文件检查器中关闭“使用Autolayout”。然后,您可以使用setFrame或setBounds方法更改高度。

+0

它工作正常,但我们将在整个应用程序中释放AutoLayout。 –

+0

您仍然可以使用自动调整功能 – Gokul

+1

启用AutoLayout后无法实现此功能吗? – MrBr

2

您可以通过为高度约束创建出口,然后在代码中调整约束常量来避免禁用Autolayout。

出口

@IBOutlet weak var collectionViewVerticalConstraint: NSLayoutConstraint! 

调整

collectionViewVerticalConstraint.constant = 0