简体   繁体   English

如何在xcode 5中创建相对约束?

[英]How to create relative constraints in xcode 5?

在此输入图像描述

Hello, i want to create relative constraints between 3 elements. 您好,我想在3个元素之间创建相对约束。 And when resized from 4 to 3.5inch those constraints resizing to the new size while objects keep their size; 当从4到3.5英寸调整大小时,这些约束会在对象保持其大小的同时调整为新大小;

There is a way to create flexible spacing between elements with the help of constraints. 有一种方法可以在约束的帮助下在元素之间创建灵活的间距。 The way is to use a view for the spacing not a constraint. 方法是使用视图作为间距而不是约束。 There is event a sample in the official documentation. 官方文档中有一个事件样本。

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html

Look at the section named "Spacing and Wrapping". 查看名为“Spacing and Wrapping”的部分。

Don't use static height and width. 不要使用静态高度和宽度。 use following code for calculating height and width. 使用以下代码计算高度和宽度。

int width = self.view.frame.size.width;
int height = self.view.frame.size.height;

In this way set X and Y co-ordinate for your element. 通过这种方式设置元素的X和Y坐标。

Refer this Code - 参考此代码 -

int imageX = 2,imageY = 2;
int count1 = 0;

for (int i = 0; i < [mainMenuColumn1Array count]; i++) {

    count1++;

    MenuClass *menuClass = [[MenuClass alloc] init];
    menuClass = [mainMenuColumn1Array objectAtIndex:(count1 - 1)];

    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(imageX, imageY, (width/2)-4, (height/3)-4);
    menuBtn.tag = count1;
    [menuBtn addTarget:self action:@selector(mainMenu1Action:) forControlEvents:UIControlEventTouchUpInside];
    menuBtn.backgroundColor = [UIColor colorWithRed:17.0/255.0 green:116.0/255.0 blue:239.0/255.0 alpha:1.0];
    [mainView1 addSubview:menuBtn];

    imageY = imageY + height/3;
    imageX = 2;

}

Here I have add UIButtons dynamically. 这里我动态添加了UIButtons And I set XY co-ordinates dynamically. 我动态设置XY坐标。 This is a generic code for all size devices. 这是适用于所有尺寸设备的通用代码。

You can't do this in interface builder, as far as I am aware, because you can't specify multipliers on constraints via interface builder. 据我所知,您无法在界面构建器中执行此操作,因为您无法通过界面构建​​器在约束上指定乘数。 However, you can do it quite easily in code, particularly using a nice auto layout helper category available via Github or cocoapods . 但是,您可以在代码中轻松完成,特别是使用通过Github或cocoapods提供漂亮的自动布局助手类别 (Disclaimer - I wrote the category!). (免责声明 - 我写了这个类别!)。

The category contains a method to distribute an array of views evenly along a specified axis, and under the hood it creates constraints using multipliers of the containing view's dimensions - so for two views, the centres would be 0.33 and 0.66 of the way along the relevant axis, for example. 该类别包含一个沿指定轴均匀分布视图数组的方法,并且它使用包含视图维度的乘数创建约束 - 因此对于两个视图,中心将是相关路径的0.33和0.66例如,轴。

To use this for a view primarily built in interface builder, you'd use placeholder constraints (removed at run time) then add the category constraints after viewDidLoad. 要将此用于主要在界面构建器中构建的视图,您将使用占位符约束(在运行时删除),然后在viewDidLoad之后添加类别约束。

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

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