xcode 调整 UICollectionView 高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13017528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:53:58  来源:igfitidea点击:

Resize UICollectionView Height

xcoderesizecgrectuicollectionview

提问by Neil Faulkner

I'm trying to resize a UICollectionView height by setting it to 0 when the view controller is loaded, then increasing its size with an animation when a button is pressed. I've tried a few different things but it doesn't change in size at all. Here's all the different things I've tried to change its height to 0:

我试图通过在加载视图控制器时将其设置为 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)];

....

....

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;

回答by pav

If you are using Interface Builder for UICollectionView initialisation, switch off "Use Autolayout" from the File Inspector in the xib file where the your CollectionView is created. Then you can change the height with setFrame or setBounds methods.

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

回答by sheepgobeep

You can avoid disabling Autolayout by creating an outlet for the height constraint and then adjusting the constraint's constant in code.

您可以通过为高度约束创建一个出口,然后在代码中调整约束的常量来避免禁用自动布局。

Outlet

出口

@IBOutlet weak var collectionViewVerticalConstraint: NSLayoutConstraint!

Adjustment

调整

collectionViewVerticalConstraint.constant = 0