14

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

Mostafa
  • 1,522
  • 2
  • 18
  • 34
  • check http://stackoverflow.com/a/30589956/3633534 – Sujay Jun 26 '15 at 13:30
  • Create the margin using a transparent view between the Done button and the bottom. Pin the view to the sides, Done button and the bottom. Now give it an aspect ratio constraint based on the height you want for iPhone 4. Although not perfect, this will scale with width which generally scales in the same proportion as height. So if you made the apsect so its 100 high on iPhone 4, it would be 100 on iPhone 5, 117 on iphone 6 and 129 in iphone6+ and 240 on iPad. So other than iPhone 5, this gives you what you want. – Rory McKinnel Jun 26 '15 at 13:43
  • @RoryMcKinnel Is there is any cleaner "Direct" way to do it ? I feel its a workaround – Mostafa Jun 26 '15 at 13:53
  • @MostafaMohamedRaafat The clean way is to CTRL drag the height constraint into your controller header file and create an IBOutlet. Then in `viewDidLoad` set the constraints constant to a percentage of the screen height. Sadly IB does not support calculated constraints relative to other constraints beyond aspect ratio and equal widths/heights. My advice would be to do it with a height constraint you set in code. Very easy to do. – Rory McKinnel Jun 26 '15 at 14:09

3 Answers3

9

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 !

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
GaétanZ
  • 4,870
  • 1
  • 23
  • 30
4

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.

Community
  • 1
  • 1
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86
1

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;

  ...
}
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • I don't think this is correct, as this won't update the margin if the view size changes will it? E.g. going from portrait orientation to landscape orientation. – Paludis Sep 16 '15 at 02:31
  • @Paludis This code only shows the idea and does not cover rotation. When you have rotation and you have created constraints with fixed sizes based on the orientation, you have to add the necessary rotation method overrides and adjust the constraints as necessary. Layout then takes care of the rest on the next layout cycle. You can also implement the change in `viewWillLayoutSubviews` which covers rotation as well. – Rory McKinnel Sep 16 '15 at 09:06
  • @RoryMcKinnel, The task is to set the margin's size proportional to the view's height. – adnako Nov 07 '18 at 10:35
  • @adnako Last I checked, code that sets a margin as a percentage of the screen height does that! – Rory McKinnel Nov 07 '18 at 20:44
  • @RoryMcKinnel, Yes, you're right, it sets margins height – adnako Nov 09 '18 at 05:27