ios - autolayout 可以自动布局这个(附图)吗?

标签 ios interface-builder autolayout

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

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

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

我是否需要手动重新定位 willRotateToInterfaceOrientation 中的 subview ?或者 Autolayout 可以自动为我完成吗?

  • B 是一个始终位于右下方的按钮
  • A 是相对于 B 定位的按钮(最好对齐到 C 的左侧)
  • C 为文字,大小灵活
  • ABC
  • 下面

最佳答案

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

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

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

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

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

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

对于横向,您可以反转这些约束:

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

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

关于ios - autolayout 可以自动布局这个(附图)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17960397/

相关文章:

ios - 无法同时满足约束。前导和尾随约束冲突

ios - 在 iPhone Mixer EQGraph 测试中使用 AUGraph 转发歌曲

android - WebRTC 使用 Peerjs 和原生 Android/iOS 客户端

iphone - IB - 无法为 View Controller 分配类

ios - xcode Interface Builder 未更新 IBDesignable 类

ios - UICollectionView 单元格在删除具有 estimatedItemSize 的项目时调整大小

ios - UIScrollView 无法以编程方式使用自动布局(使用多个图像)

ios - 尝试更改导航 Controller 上的标题和后退按钮等属性

iOS - 如何将 Firebase Analytics 与现有的 Google Analytics 集成,GoogleService-Info.plist

ios - UITableViewCell 对齐列以适应内容