本文介绍了如何使用自动布局在屏幕上水平和垂直居中放置UILabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用自动布局两天了,我试图将UILabel在屏幕上垂直和水平居中,但是我对将标签居中放在中间并没有多大运气.

I've been using auto layout for a couple of days now, and I am trying to center a UILabel in the screen vertically and horizontally, but I am not having much luck with getting the label centered.

我希望实现类似于以下内容的

I am hoping to achieve something that looks like the following,

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

我向UILabel添加了以下约束,

I added the following constraints to the 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];

推荐答案

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

更新:Apple现在希望您使用[NSLayoutConstraint activateConstraints:][NSLayoutConstraint deactivateConstraints:]而不是使用[UIView addConstraint:][UIView removeConstraint:].

UPDATE: Apple now wants you to use [NSLayoutConstraint activateConstraints:] and [NSLayoutConstraint deactivateConstraints:] instead of using [UIView addConstraint:] and [UIView removeConstraint:].

UPDATE 8/24/17:

UPDATE 8/24/17:

可以使用布局锚来完成此操作的一个简单版本:

A simpler version of this can be done using layout anchors:

[label.centerXAnchor constraintEqualToAnchor:label.superview.centerXAnchor].active = YES;
[label.centerYAnchor constraintEqualToAnchor:label.superview.centerYAnchor].active = YES;

这篇关于如何使用自动布局在屏幕上水平和垂直居中放置UILabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:42