Aleksandrenko
Aleksandrenko

Reputation: 3065

How to create relative constraints in xcode 5?

enter image description here

Hello, i want to create relative constraints between 3 elements. And when resized from 4 to 3.5inch those constraints resizing to the new size while objects keep their size;

Upvotes: 4

Views: 1213

Answers (3)

Aleksandrenko
Aleksandrenko

Reputation: 3065

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

Look at the section named "Spacing and Wrapping".

Upvotes: 2

jrturton
jrturton

Reputation: 119242

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. (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.

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.

Upvotes: 0

iSmita
iSmita

Reputation: 1292

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.

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. And I set XY co-ordinates dynamically. This is a generic code for all size devices.

Upvotes: 0

Related Questions