2012-10-22 185 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