Ios 使用自动布局Xcode对中

Ios 使用自动布局Xcode对中,ios,xcode,swift,uilabel,autolayout,Ios,Xcode,Swift,Uilabel,Autolayout,我正在开发一个swift应用程序,并在Xcode的interface builder中设计接口。我的界面(由两个UILabel组成),在纵向模式下看起来不错,但在横向模式下看起来很糟糕 问题是旋转时,标签不会像我希望的那样保持居中。我使用了以下自动布局约束: Upper edge constraint: top layout guide Lower edge constraint: bottom layout guide Right edge constraint: standard valu

我正在开发一个swift应用程序,并在Xcode的interface builder中设计接口。我的界面(由两个UILabel组成),在纵向模式下看起来不错,但在横向模式下看起来很糟糕

问题是旋转时,标签不会像我希望的那样保持居中。我使用了以下自动布局约束:

Upper edge constraint: top layout guide 
Lower edge constraint: bottom layout guide
Right edge constraint: standard value
Left edge constraint: standard value

这些设置不能工作,因为旋转时标签不能保持居中。自动布局要使标签居中,需要使用哪些适当的约束?

如果可以在interface builder中实现所需的效果,我建议您使用编程自动布局约束

[self.view addConstraint: [NSLayoutConstraint
    constraintWithItem:self.view 
    attribute:NSLayoutAttributeCenterX 
    relatedBy:NSLayoutRelationEqual 
    toItem:"YOUR VIEW" 
    attribute:NSLayoutAttributeCenterX 
    multiplier:1
    constant:0]];

上述NSLAYOUTATTributeCenter属性使“您的视图”水平居中。对于垂直居中布局,您可以将self.VIEW和“您的视图”的属性属性更改为NSLAYOUTATTributeCenter.

谢谢!几个问题-我可以同时在水平和垂直方向上设置代码中心吗?还有,我应该把代码放在哪里?就在IBoutlet下面,或者其他地方?对不起,我只是在学习swift!恐怕你不能像我所知的那样快速。你需要为水平和垂直居中设置两次约束。code应该放在viewDidLoad方法中。