Ios 将UIView在其父视图内垂直和水平居中

Ios 将UIView在其父视图内垂直和水平居中,ios,objective-c,uiview,uicollectionview,frame,Ios,Objective C,Uiview,Uicollectionview,Frame,我有一个UICollection视图,它位于另一个UIView中。我正在尝试将集合视图置于其父视图的中心。当前通过调整其框架手动执行此操作(请参见下面的代码)。必须有一个更简单的方法 当前代码: self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(COLLECTION_PADDING+10, COLLECTION_PADDING+5, self.bounds.size.width - (COLLECTI

我有一个UICollection视图,它位于另一个UIView中。我正在尝试将集合视图置于其父视图的中心。当前通过调整其框架手动执行此操作(请参见下面的代码)。必须有一个更简单的方法

当前代码:

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(COLLECTION_PADDING+10, COLLECTION_PADDING+5, self.bounds.size.width - (COLLECTION_PADDING*2), self.bounds.size.height - (COLLECTION_PADDING+22)) collectionViewLayout:flow];

在我看来,您可以尝试找出collectionview和父视图大小之间的差异。然后除以2,用它填充所有边

parentHeight = self.bounds.size.height;
parentWidth = self.bounds.size.width;
heightDiff = parentHeight - collectionview.bounds.size.height;
widthDiff = parentWidth - collectionview.bounds.size.width;
heightPadding = heightDiff/2;
widthPadding = widthDiff/2;

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(widthPadding, heightPadding, parentWidth-widthPadding, parentHeight-heightPadding) collectionViewLayout:flow];

您可以设置collectionView的宽度和高度,然后使用
center
属性调整其位置

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,width,height)];
self.collectionView.center = CGPointMake(self.center.x,self.center.y);
编辑: 正如Logan在评论中提到的,只有当self和self.collectionView具有相同的superview时,这才有效。如果要将collectionView添加到视图中,并希望它位于该视图的中心,则可以执行以下操作:

self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,width,height)];
self.collectionView.center = CGPointMake(self.center.x - self.frame.origin.x/2,self.center.y - self.frame.origin.x/2);

但当涉及旋转时,这种情况不起作用。在这种情况下,你将不得不使用其他方法。因此,与编码中的任何内容一样,这并不是一个明确的答案。但在大多数情况下,它会起作用。如果您遇到这种罕见的情况,即它不起作用,那么您可能会做更多的自定义框架,而不仅仅是在父级内部居中。

您可以这样做:

self.collectionView.center = CGPointMake(CGRectGetMidX(superview.bounds), CGRectGetMidY(superview.bounds));
self.collectionView.center = superview.center
Swift 3:

self.collectionView.center = CGPoint(superview.bounds.midX, superview.bounds.midY)
使用这种方法,无论superview的坐标如何,子视图都将在其superview中居中。使用这样的方法:

self.collectionView.center = CGPointMake(CGRectGetMidX(superview.bounds), CGRectGetMidY(superview.bounds));
self.collectionView.center = superview.center

如果superview的原点不是0,0,则会导致问题。这可能会对您有所帮助

这是用于水平对齐的

NSLayoutConstraint *centerHcons =[NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:collectionViewSuper attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
NSLayoutConstraint *centerVcons =[NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:collectionViewSuper attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];

    [collectionViewSuper addConstraints:@[centerHcons, centerVcons]];
这是用于垂直对齐的

NSLayoutConstraint *centerHcons =[NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:collectionViewSuper attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f];
NSLayoutConstraint *centerVcons =[NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:collectionViewSuper attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];

    [collectionViewSuper addConstraints:@[centerHcons, centerVcons]];

请澄清您的具体问题或添加其他详细信息,以突出显示您所需的内容。正如目前所写的,很难说清楚你在问什么。你是如何创建和定位视图的?你在使用界面生成器吗?自动布局?Struts?这里有很多遗漏的信息。把它擦掉,普茨的答案更好。忽略我:)不一定更好,只是更简单。人们似乎喜欢更简单。虽然这通常会起作用,但当superview的原点不是0,0时,会出现问题。交替的情况会导致问题。另外,
CGPointMake(self.center.x,self.center.y)等于
self.center
。使用swift 4.2:self.collectionView.center=CGPoint(x:superview.bounds.midX,y:superview.bounds.midY)