Aggressor
Aggressor

Reputation: 13551

How To Get Leading Space To Be 1/3 The Width Of the SuperView

I am trying to set an icon to be 1/3 along the width of the superview.

However when I set the width it's only give me hard values as the option.

Is it possible to set the x position of an object to be 1/3rd the width through the interface builder?

enter image description here

Thanks

Update:

Trying to set a .leading against a .width does not appear to be an option for leading:

enter image description here

Upvotes: 1

Views: 643

Answers (2)

rdelmar
rdelmar

Reputation: 104082

You should make the constraint between the icon's centerX and the superview's trailing. In the images below, first, and third refer to the first and third icons. The constraints will put the first one 1/3 of the way from the left edge, and the third one 1/3 of the way in from the right edge.

enter image description here

enter image description here

Upvotes: 5

mittens
mittens

Reputation: 746

You can constrain the widths to be equal with a multiplier of 1/3

Sorry misread question. You could do something like

// self.imageView is a reference to the image view (or view, whatever it is)
// and self.leadingConstraint is a constraint on the imageView's leading/X constraint
// probably have to recalculate on rotation (maybe, didn't test)
CGFloat constant = self.imageView.superview.frame.size.width / 3;
self.leadingConstraint.constant = constant;
[self.view layoutIfNeeded];

In IB

Not perfect but ¯_(ツ)_/¯

higher the number, further left it goes

Upvotes: 0

Related Questions