如何使用自动布局在屏幕中水平和垂直居中UILabel?

时间:2015-04-30 19:07:06

标签: ios objective-c autolayout

我现在已经使用了自动布局几天了,我试图将UILabel垂直和水平地放在屏幕中,但我没有太多运气让标签居中。

我希望能够实现如下所示的内容,

  ---------------
 |               |
 |               |
 |               |
 |               |
 |    Label      |
 |               |
 |               |
 |               |
 |               |
 | SIGNIN   REG  |
  ---------------

我向UILabel添加了以下约束,

NSLayoutConstraint *myLabelConstraintHeight = [NSLayoutConstraint constraintWithItem:myLabel
                                                                          attribute:NSLayoutAttributeHeight
                                                                          relatedBy:NSLayoutRelationEqual toItem:nil
                                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                                         multiplier:1.0f
                                                                           constant:100];

            [myLabel addConstraint:myLabelConstraintHeight];


            NSLayoutConstraint *myLabelConstraintWidth = [NSLayoutConstraint constraintWithItem:myLabel
                                                                                      attribute:NSLayoutAttributeWidth
                                                                                      relatedBy:NSLayoutRelationEqual toItem:nil
                                                                                      attribute:NSLayoutAttributeNotAnAttribute
                                                                                     multiplier:1.0f
                                                                                       constant:100];
            [myLabel addConstraint:myLabelConstraintWidth];

1 个答案:

答案 0 :(得分:7)


 private void SeedDatabaseActivities(SiteDbContext context)
    {
            context.Activities.AddOrUpdate(new ActivityDefinition
            {
                Id = 605,
                Name = "bicycling, >20 mph, racing, not drafting",
                UseSteps = false,
                UseDistance = false,
                Category = "bicycling",
                Mets = 16
            }); 
}

更新:Apple现在希望您使用NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:label.superview attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]; NSLayoutConstraint *centerY = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:label.superview attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]; // No longer using [label addConstraints:@[centerX, centerY]; [NSLayoutConstraint activateConstraints:@[centerX, centerY]]; [NSLayoutConstraint activateConstraints:],而不是使用[NSLayoutConstraint deactivateConstraints:][UIView addConstraint:]

更新8/24/17:

使用布局锚点可以完成更简单的版本:

[UIView removeConstraint:]