简体   繁体   English

以自动布局Xcode为中心

[英]Centering with Auto layout Xcode

I'm developing a swift app, and I am designing the interface in interface builder in Xcode. 我正在开发一个快速的应用程序,我正在Xcode的界面构建器中设计界面。 My interface (which consists of two UILabels, looks fine in portrait mode, but looks awful in landscape mode. 我的界面(由两个UILabel组成,在纵向模式下看起来很好,但在横向模式下看起来很糟糕。

The issue is that the labels won't stay centered, as I want them, when rotated. 问题是标签在旋转时不会像我想要的那样保持居中。 I used the following auto layout constraints: 我使用了以下自动布局约束:

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

These settings must not be working, as the label doesn't stay centered when rotated. 这些设置必须无法正常工作,因为旋转时标签不会保持居中。 What are the proper constraints to use for auto layout to center this label? 用于自动布局以使此标签居中的适当约束是什么?

You can achieve the required effect in interface builder, I would advise you to do it using programmatic auto layout constraint. 您可以在界面构建器中实现所需的效果,我建议您使用程序化自动布局约束来实现。

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

The above NSLayoutAttributeCenterX attribute centred the "YOUR VIEW" horizontally. 上面的NSLayoutAttributeCenterX属性水平居中于“你的视图”。 For vertical centred layout, you can do so by changing the attribute property to NSLayoutAttributeCenterY for both self.view and "YOUR VIEW". 对于垂直居中布局,您可以通过将属性属性更改为NSLayoutAttributeCenterY来实现自我视图和“您的视图”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM