【问题标题】:Can autolayout layout this (diagram attached) automatically?自动布局可以自动布局这个(附图)吗?
【发布时间】:2013-07-31 09:20:33
【问题描述】:

要像这样支持UIView 的纵向和横向:

  +-------------------+
  | +---------------+ |
  | |               | |
  | |     Fixed     | |
  | |      size     | |
  | |     Square    | |
  | |               | |
  | +---------------+ |   Protrait
  | +---------------+ |
  | |   Flexible C  | |
  | +---------------+ |
  | +---+       +---+ |
  | | A |       | B | |
  | +---+       +---+ |
  +-------------------+

  +----------------------------------+
  | +---------------+ +------------+ |
  | |               | |  Flexible  | |
  | |    Fixed      | |     C      | |
  | |     size      | +------------+ |
  | |    Square     | +---+    +---+ |
  | |               | | A |    | B | |
  | +---------------+ +---+    +---+ |
  +----------------------------------+
               Horizontal

我需要手动重新定位willRotateToInterfaceOrientation 中的子视图吗?或者Autolayout可以自动帮我做吗?

  • B 是一个总是停留在右下方的按钮
  • A 是相对于B 定位的按钮(最好与C 的左侧对齐)
  • C 用于文本,大小灵活
  • AB 低于 C

【问题讨论】:

  • 我看不出没有自定义代码自动布局如何做到这一点。不过我可能错了。不过,您只需删除并设置一个约束。旋转到横向时,去掉弹性C和固定大小正方形之间的垂直对齐约束,给c添加一个顶部对齐约束

标签: ios interface-builder autolayout


【解决方案1】:

在这种情况下,您应该能够自动完成大部分布局工作,但需要在旋转时进行一些细微调整。

我的建议是在另一个 UIView包含视图 A、B 和 C。这样,与方向相关的布局与灵活大小的布局分开。它还使布局更易于编码!

然后你像这样布置方形视图和容器视图:

H:|-10-[squareView]
V:|-10-[squareView]
H:[containerView]-10-|
V:[containerView]-10-|
squareView.width == squareView.height

请注意,方形视图始终与父视图的左侧和顶部对齐,而容器视图与底部和右侧对齐。对于纵向,您将添加以下约束:

V:[squareView]-10-[containerView]
H:[squareView]-10-|
H:|-10-[containerView]

对于横向,您可以颠倒这些约束:

H:[squareView]-10-[containerView]
V:[squareView]-10-|
V:|-10-[containerView]

这只是针对整体布局,因此容器视图的子视图的灵活大小取决于您。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2013-06-17
    • 2023-03-24
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多