Xcode AutoLayout,使边距与高度成比例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31074431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:22:57  来源:igfitidea点击:

Xcode AutoLayout, Make Margin proportional with height

iosxcodeautolayout

提问by Mostafa Mohamed Raafat

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

我很想弄清楚如何使两个对象的边距成比例。如果我在 Xcode 上选择纵横比,它会为我提供调整对象比例屏幕高度的选项。这不是我的情况。我希望按钮和屏幕底部之间的边距与屏幕高度成正比。我希望它使用 Xcode“无代码”来完成。我想要做什么

Your support is highly appreciated.

非常感谢您的支持。

Thank you

谢谢

采纳答案by GaétanZ

Use the multiplier property of NSLayoutConstraint !

使用 NSLayoutConstraint 的 multiplier 属性!

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.

首先,使用您希望的乘数设置从您的视图到其超视图的等高约束。您可以使用 Ctrl + 从视图拖动到超级视图。

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.

然后在从视图顶部到父视图的 0 处设置另一个约束,并从视图到父视图设置一个等宽约束,乘数为 1。

Done !

完毕 !

回答by ChikabuZ

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

添加透明视图并使其与高度成比例。用这个透明视图绑定你完成按钮。在那里你可以找到如何制作它。

回答by Rory McKinnel

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:

我知道您不需要任何代码,但我认为您最好使用高度约束。为边距添加高度约束,然后按 CTRL 将其拖入您的头文件中以获得类似以下内容:

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

然后在 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;

  ...
}