Mostafa
Mostafa

Reputation: 1612

Xcode AutoLayout, Make Margin proportional with height

I am having a big time figuring out how to make the margin of two objects proportional. If i choose aspect ratio on Xcode it gives me the option for adjust the height of my object proportional screen height. and thats not my case. I want the margin between a button and screen bottom to be proportional to screen height. And i want it to be done using Xcode "No Code".What I am seeking to do

Your support is highly appreciated.

Thank you

Upvotes: 14

Views: 7261

Answers (3)

GaétanZ
GaétanZ

Reputation: 4930

Use the multiplier property of NSLayoutConstraint !

First, set the equal height constraint from your view to its superview with the multiplier you wish. You can use Ctrl + drag from your view to the superview.

Then set another constraint at 0 from the top of your view to the superview and a equal width constrain with a multiplier to 1 from your view to the superview.

Done !

Upvotes: 9

ChikabuZ
ChikabuZ

Reputation: 10215

Add transparent view and make it proportional to height. Bind you Done button with this transparent view. There you can find how to make it.

Upvotes: 4

Rory McKinnel
Rory McKinnel

Reputation: 8014

I know you wanted no code, but I think you would be best to use a height constraint. Add a constraint for height for the margin and CTRL drag it into your header file to get something like:

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *myMarginHeightConstraint;

You need a property which contains the fraction of the screen height for the margin:

@property CGFloat myHeightFraction;

Then add this in viewDidLoad.

- (void)viewDidLoad
{
  [super viewDidLoad];

  self.myHeightFraction = 0.1; // e.g to get margin at 10% of height
  myMarginHeightConstraint.constant = myHeightFraction * [[UIScreen mainScreen] bounds].size.height;

  ...
}

Upvotes: 1

Related Questions