UIStoryboard自动布局问题与约束

时间:2012-10-25 20:20:36

标签: objective-c xcode constraints uistoryboard autolayout

非常简单(我希望)autolayout问题,我正在敲打我的iMac。

我有这个肖像

enter image description here

这就是景观中发生的事情。

enter image description here

我想要的只是标签展开,就像你没有使用autolayout一样。 我添加了哪些约束来均匀分隔它们?

4 个答案:

答案 0 :(得分:2)

有一种解决方案比@ sust86:

更简单,也更灵活
  1. 在标签之间引入用作 spring 的空视图。
  2. 使用恒定距离约束连接所有相邻视图。
  3. 将其中一个新的弹簧视图与所有其他具有相同宽度约束的弹簧视图连接。
  4. 删除所有不需要的约束。
  5. 对我而言,约束看起来与此相似:

    enter image description here

    此配置的优点是,当标签的宽度(在我的情况下为按钮)发生变化时,您无需修复任何间距。

答案 1 :(得分:1)

在iOS 6中使用新约束非常棘手。有关与iOS 6中的自动布局相关的ray网站上有一个很好的2部分教程。虽然它解释了如何将图像持有者锚定在自动布局中,但这些原则帮助我理解了整个自动布局。希望这对你也有帮助。 Herer是链接: http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2 阿德里安

答案 2 :(得分:1)

试试这个:

#define moneyLabelWidth 20
@property (nonatomic, strong) UILabel* moneyOneLabel;
@property (nonatomic, strong) UILabel* moneyTwoLabel;
@property (nonatomic, strong) UILabel* moneyThreeLabel;
@property (nonatomic, strong) UILabel* moneyFourLabel;
@property (nonatomic, strong) UILabel* moneyFiveLabel;
@property (nonatomic, strong) UILabel* moneySixLabel;
...
[self.view addSubview:moneyOneLabe];
[self.view addSubview:moneyTwoLabe];

moneyOneLabel.translatesAutoresizingMaskIntoConstraints = NO;
moneyTwoLabel.translatesAutoresizingMaskIntoConstraints = NO
etc
...


[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_moneyOneLabel(moneyLabelWidth)]-[_moneyTwoLabel(moneyLabelWidth)]-[_moneyThreeLabel(moneyLabelWidth)]-[_moneyFourLabel(moneyLabelWidth)]-[_moneyFiveLabel(moneyLabelWidth)]-[_moneySixLabel(moneyLabelWidth)]-|"
                                                                  options:0
                                                                  metrics:@{@"moneyLabelWidth":@(moneyLabelWidth)}
                                                                    views:NSDictionaryOfVariableBindings(_moneyOneLabel, _moneyTwoLabel, _moneyThreeLabel, _moneyFourLabel, _moneyFiveLabel, moneySixLabel)]];
/*add vertical constraints*/

上面的约束与视角格式基本上说从左边缘到右边缘水平地绘制了6个钱币标签,全部宽度为20,宽度可变。我没有运行此代码,但我认为它会起作用(或至少接近)。

答案 3 :(得分:1)

最简单的方法是在每个视图之间使用 2约束。一个大于或等于,一个 Less Then或Equal 。 最小尺寸(Greater Then或Equal)应为纵向模式下的间距。 最大尺寸(Lesser Then或Equal)应该是横向模式中的间距。

这与我在这个问题上提供的解决方案相同:

Using Auto Layout to change spaces equally on resizing