调整UICollectionView高度的大小

Nei*_*ner 8 xcode resize cgrect uicollectionview

我正在尝试通过在加载视图控制器时将其设置为0来调整UICollectionView高度,然后在按下按钮时通过动画增加其大小.我尝试了一些不同的东西,但它的大小根本没有变化.以下是我尝试将其高度更改为0的所有不同内容:

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

....

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

....

CGRect frame = self.collectionView.frame;
frame.size.height -= 100;
self.collectionView.frame = frame;
Run Code Online (Sandbox Code Playgroud)

....

 self.collectionView.clipsToBounds = YES;
 self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
Run Code Online (Sandbox Code Playgroud)

小智 4

如果您使用 Interface Builder 进行 UICollectionView 初始化,请从创建 CollectionView 的 xib 文件中的文件检查器中关闭“使用自动布局”。然后您可以使用 setFrame 或 setBounds 方法更改高度。