简体   繁体   English

通过自动布局更改约束

[英]Changing constraints with auto layout

I have a grid of square buttons in a view, that fits perfectly on the iPhone 5. I now want it to look good on the 3.5-inch screen as well. 我在视图中有一个方形按钮网格,非常适合iPhone5。我现在也希望它在3.5英寸屏幕上也看起来不错。 I am using auto layout, and I have created new images for the 3.5-inch, that have a bit smaller height, so that they will fit on the screen (they are not squares anymore). 我正在使用自动布局,并且为3.5英寸创建了新的图像,这些图像的高度略小,以便它们可以在屏幕上显示(它们不再是正方形了)。 From what I have come to understand you can not use frames as usual when using auto layout? 据我了解,使用自动布局时不能像往常一样使用框架吗? Instead, you should use constraints. 相反,您应该使用约束。 But how would I do that? 但是我该怎么做呢?

Basically what I need to do is to change the y-coordinate and the height of the buttons. 基本上,我需要做的是更改y坐标和按钮的高度。

This is an example of what the screen looks like on the 4-inch screen (tab bar in the bottom). 这是4英寸屏幕(底部的标签栏)上的屏幕示例。

例

Representing this in VFL, what you want is something like: 用VFL表示这一点,您想要的是:

Make the vertical red boxes vertically evenly sized, spanning the vertical span of their superview: 使垂直红色框的尺寸垂直均匀,并覆盖其超级视图的垂直范围:

@"V:|-[subview1]-[subview2(==subview1)]-[subview3(==subview1)]-|"
@"V:|-[subview4]-[subview5(==subview4)]-[subview6(==subview4)]-|"

Likewise, make the boxes horizontally evenly sized, spanning the horizontal span of their superview, too: 同样,也要使框的尺寸水平均匀,并跨越其视图的水平范围:

@"H:|-[subview1]-[subview4(==subview1)]-|"
@"H:|-[subview2]-[subview5(==subview2)]-|"
@"H:|-[subview3]-[subview6(==subview3)]-|"

That makes the six subviews equally sized and evenly spaced within their superview. 这使得六个子视图在其父视图中具有相等的大小和均匀的间距。 And this illustrates one of the advantages of auto layout because we, quite explicitly, avoid any reference to y or height values. 这说明了自动布局的优点之一,因为我们非常明确地避免了对yheight值的任何引用。 We only have to define the relationship between views, their siblings, and their superview, and auto layout takes care of the rest. 我们只需要定义视图,它们的兄弟姐妹和它们的超级视图之间的关系,其余部分就由自动布局负责。

This pattern applies whether you do it programmatically with VFL like above, or in Interface Builder, but hopefully it illustrates the concept. 无论您是使用上述VFL还是在Interface Builder中以编程方式进行操作,此模式都适用,但希望它能说明这一概念。

Alternatively, you can use a UICollectionView , which is designed for spacing out these controls, and you can set it up so it correctly adjusts the layout for portrait and landscape. 另外,您可以使用UICollectionView ,它是用于分隔这些控件的,可以对其进行设置,以便它可以正确地调整纵向和横向的布局。


For example, to add six image views to my view, I can add QuartzCore.framework to project (which I can use to create the red image view background with rounded corners), then import the header: 例如,要将六个图像视图添加到视图中,可以将QuartzCore.framework添加到项目中(可用于创建带有圆角的红色图像视图背景),然后导入标头:

#import <QuartzCore/QuartzCore.h>   // and 

And then in viewDidLoad : 然后在viewDidLoad

NSMutableArray *subviews = [NSMutableArray array];

for (NSInteger i = 0; i < 6; i++)
{
    UIImageView *subview = [[UIImageView alloc] init];
    [subviews addObject:subview];
    subview.layer.cornerRadius = 10.0;
    subview.backgroundColor = [UIColor redColor];
    subview.contentMode = UIViewContentModeCenter;
    subview.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:subview];
}

NSDictionary *views = @{@"subview1": subviews[0],
                        @"subview2": subviews[1],
                        @"subview3": subviews[2],
                        @"subview4": subviews[3],
                        @"subview5": subviews[4],
                        @"subview6": subviews[5]};

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[subview1]-[subview2(==subview1)]-[subview3(==subview1)]-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[subview4]-[subview5(==subview4)]-[subview6(==subview4)]-|" options:0 metrics:nil views:views]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[subview1]-[subview4(==subview1)]-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[subview2]-[subview5(==subview2)]-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[subview3]-[subview6(==subview3)]-|" options:0 metrics:nil views:views]];

在此处输入图片说明

Then, if I use PNG images with a transparent background, I can set the image property for these six image views, letting auto layout define the frame , QuartzCore will do the nicely rounded corners, etc. 然后,如果我使用具有透明背景的PNG图像,则可以为这六个图像视图设置image属性,让自动布局定义frame ,QuartzCore会做圆角,等等。

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

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