Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用自动布局在屏幕上水平和垂直居中设置UILabel?_Ios_Objective C_Autolayout - Fatal编程技术网

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

Ios 如何使用自动布局在屏幕上水平和垂直居中设置UILabel?,ios,objective-c,autolayout,Ios,Objective C,Autolayout,我已经使用auto layout好几天了,我正在尝试在屏幕上垂直和水平居中放置UILabel,但是我没有太多的运气将标签居中 我希望能实现如下目标: --------------- | | | | | | | | | Label | | | | | | | |

我已经使用auto layout好几天了,我正在尝试在屏幕上垂直和水平居中放置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];
更新:Apple现在希望您使用
[NSLayoutConstraint activateConstraints:
[NSLayoutConstraint DISACTIVATECONSTRAINTS:
而不是使用
[UIView addConstraint:

2017年8月24日更新:

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

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

NSLayoutConstraint constraintWithItem:myLabel属性:NSLayoutAttributeCenter relatedBy:NSLayoutRelationEqual toItem:myLabel.superview属性:NSLayoutAttributeCenter乘数:1.f常数:0.f];对center Y执行同样的操作如果我使用以下行将
myLabel
添加到视图层次结构中,
[self.view addSubview:myLabel]?不,不会,只要将标签添加为子视图,然后添加约束。否则,label.superview可能为零。
[label.centerXAnchor constraintEqualToAnchor:label.superview.centerXAnchor].active = YES;
[label.centerYAnchor constraintEqualToAnchor:label.superview.centerYAnchor].active = YES;