Xcode 6以编程方式自动布局? [英] Xcode 6 auto layout programmatically?

查看:83
本文介绍了Xcode 6以编程方式自动布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题在这张图片上:

My problem is on this image:

我在Xcode 6中遇到这个问题,我想知道是否有一种方法可以通过编程方式解决此问题...我想编写一个使我的视图控制器适应所有设备的代码...是否可能?? (我已经尝试过手动进行自动布局,但是我真的很糟糕)...

I have this problem with Xcode 6 and I would like to know if there is a way to solve this problem programmatically... I would like to write a code which adapt my view controller to all devices... Is it possible?? (I already tried auto layout manually but I am really bad)...

推荐答案

似乎您实施了过于严格的约束.正如我在屏幕截图中看到的那样,您可能已经为按钮和输入设置了很宽的宽度约束,而不是等宽或等距的间距.使约束更加灵活.首选>=<=约束,等宽约束,前导/尾随约束.与严格的布局相比,您将需要更多的布局,但是您将获得真正的自适应布局.

Seems like you've implemented too strict constraints. As I see on screenshot, you've probably set a strong width constraint for your buttons and inputs instead of equal widths or equal spacing between each other. Make your constrains more flexible. Prefer >=,<= constraints, equal widths constraints, leading/trailing constraints. You will need more of them in comparison to strict ones, but you'll get a true adaptive layout.

要在视图之间灵活地保持相等的间距,您可能会发现间隔视图的概念很有用.简而言之,放置一个透明的UIView并为其设置一些约束.要了解更多信息,请阅读示例自动布局"

To get flexible equal spacing between views, you might find helpful a conception of spacer views. In short, place a transparent UIView and set some constraints to it. To know more, read apple docs. If you like videos more (like I do :)) - watch wwdc videos, they are great. For example, Auto Layout by Example or Taking Control of Auto Layout in Xcode 5.

以均等排列的按钮为例,请看以下屏幕截图: 这是通过设置一些复杂但可理解的约束来完成的:

As an example of equally arranged buttons take a look at this screenshot: This is done with a little bit complex, but understandable constraints set:

所有按钮彼此之间具有相等的宽度约束.所有间隔视图彼此之间具有相等的宽度约束.每个间隔视图都有对按钮或父视图的引线/尾部的前导/尾随约束.

All the buttons has the equal widths constraints for each other. All spacer views has equal width constraints on each other. Each spacer view has leading/trailing constrains to buttons or parent view leadings/trailings.

有了iOS 9,您还有更多选择:

With iOS 9 you have a few more options:

  • 您可以使用UILayoutGuide
  • 代替间隔视图
  • UIStackView允许实现相同的布局而完全没有约束
  • instead of spacer views you can use UILayoutGuide
  • UIStackView allows to achieve the same layout with no constraints at all

我建议您观看新的WWDC 2015视频,自动布局的奥秘",第2部分

I suggest you to watch new WWDC 2015 videos, "Mysteries of Auto Layout",Part1 and Part2

这篇关于Xcode 6以编程方式自动布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆