【问题标题】:Center a UIView vertically and horizontally within its parent view在其父视图中垂直和水平居中 UIView
【发布时间】:2014-09-17 10:35:36
【问题描述】:

我有一个 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];

【问题讨论】:

  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。
  • 您是如何创建和定位视图的?你在使用界面生成器吗?自动布局?支柱?这里有很多缺失的信息。

标签: ios objective-c uiview uicollectionview frame


【解决方案1】:

IMO,您可以尝试找出您的收藏视图和父视图大小之间的差异。然后除以 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];

【讨论】:

  • 从头开始,Putz 的答案更好。无视我:)
  • 不一定更好,只是更简单。人们似乎更喜欢简单。
【解决方案2】:

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

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

编辑: 正如 Logan 刚刚在 cmets 中提到的,这仅在 self 和 self.collectionView 具有相同的超级视图时才有效。如果您将 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);

但这种情况在涉及轮换时不起作用。在这种情况下,您将不得不使用其他方式。因此,就像编码中的任何事情一样,这不是一个明确的答案。但在大多数情况下,它会起作用。如果您遇到了它不起作用的罕见情况,那么您可能会做更多的自定义框架,而不仅仅是以父母为中心。

【讨论】:

  • 虽然这通常会起作用,但当超级视图的来源不是 0,0 时就会出现问题。其他情况会导致问题。此外,CGPointMake(self.center.x,self.center.y); 等于 self.center
【解决方案3】:

你可以这样做:

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

斯威夫特 3:

self.collectionView.center = CGPoint(superview.bounds.midX, superview.bounds.midY)

使用这种方法,无论父视图的坐标如何,您的子视图都将在其父视图中居中。使用这样的方法:

self.collectionView.center = superview.center

如果 superview 的来源不是 0,0,则会导致问题

【讨论】:

  • with swift 4.2: self.collectionView.center = CGPoint(x: superview.bounds.midX, y: superview.bounds.midY)
【解决方案4】:

听说过布局?..这可能对您有所帮助。

这是用于水平对齐

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]];

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2013-05-18
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多