【问题标题】:Center views using *only* visual format?使用*仅*视觉格式的中心视图?
【发布时间】:2014-12-03 05:10:48
【问题描述】:

我正在尝试将 2 个不同高度的视图垂直居中。

UILabel *view1 = [[UILabel alloc] init];
view1.backgroundColor = [UIColor redColor];
view1.text = @"view1\nline2";
view1.textAlignment = NSTextAlignmentCenter;
view1.numberOfLines = 0;
UILabel *view2 = [[UILabel alloc] init];
view2.backgroundColor = [UIColor greenColor];
view2.text = @"view2\nline2";
view2.textAlignment = NSTextAlignmentCenter;
view2.numberOfLines = 0;
[self.view addSubview:view1];
[self.view addSubview:view2];

NSDictionary *views = @{@"view1": view1, @"view2" : view2};

[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];

for (UIView *view in views.allValues) {
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
}

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view1]-[view2(==view1)]-|"
                                                                  options:NSLayoutFormatAlignAllCenterY
                                                                  metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=20)-[view1(200)]-(>=20)-|"
                                                                  options:NSLayoutFormatAlignAllCenterY
                                                                  metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=20)-[view2(100)]-(>=20)-|"
                                                                  options:NSLayoutFormatAlignAllCenterY
                                                                  metrics:nil views:views]];

这设法使两者的中心垂直对齐,但它们位于超级视图的底部!

我想垂直居中,不仅相对于彼此,也在超级视图中。

我添加了一个这样的约束并且它有效:

[self.view addConstraint:
 [NSLayoutConstraint constraintWithItem:view1
                              attribute:NSLayoutAttributeCenterY
                              relatedBy:NSLayoutRelationEqual
                                 toItem:self.view
                              attribute:NSLayoutAttributeCenterY
                             multiplier:1
                               constant:0]];

但我想知道,这是否可以仅使用视觉格式来实现?

【问题讨论】:

  • 我用 PureLayout 做这种事情。

标签: ios visual-format-language


【解决方案1】:

是的,这是可能的,但只能通过添加间隔视图。因此,如果您创建了几个视图,我们称它们为 spacer1 和 spacer2,您可以使用此字符串将 view1 居中,

@"V:|[spacer1][view1][spacer2(==spacer1)]|"

这假设 view1 有一个固定的高度。不过我不建议这样做,我只会使用您在帖子底部显示的代码。

【讨论】:

  • 好的,我明白了。视觉格式似乎非常有限。所以它应该与“传统”约束相结合。
【解决方案2】:

如何计算“指标”中的 Y ?

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-centerY-[view1(200)]-(>=20)-|"
                                                                  options:NSLayoutFormatAlignAllCenterY
                                                                  metrics:@{@"centerY": @(CGRectGetHeight(self.view.frame)/2.0 - 100)}
                                                                    views:views]];

【讨论】:

  • 这种方法的问题是当你旋转视图时它不起作用,除非你把它放在它会被更新的地方(然后你必须先删除旧的)。
  • 也许将它与 Size 类混合在一起?
猜你喜欢
  • 2013-11-29
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多