2012-10-25 48 views
0

非常简单(我希望)自动布局问题,我对我的iMac敲了敲头。UIStoryboard自动布局问题与约束

我有这样的肖像

enter image description here

这是景观发生了什么。

enter image description here

所有我想要的是标签摊开就像他们,如果你不使用自动布局。 我添加了什么约束来均匀地将它们分开?

+0

[Autolayout Even Spacing]的可能重复(http://stackoverflow.com/questions/13075415/autolayout-even-spacing) – jrturton

+0

如果您不想或需要自动布局,请关闭它 - 文件检查器在故事板,取消选中使用自动布局。 – jrturton

+0

我可以为故事板中的一个View Controller做到这一点吗?我不想关闭它,我想学习如何解决这个问题。 –

回答

2

有一个解决方案,比@ sust86的有些简单,也比较灵活:

  1. 介绍空作为标签之间的弹簧的视图。
  2. 以恒定的距离约束连接所有相邻的视图。
  3. 将其中一个新的弹簧视图与所有其他具有相同宽度约束的弹簧视图相连接。
  4. 删除任何不需要的约束。

对我的约束则类似于此:

enter image description here

该配置具有不需要解决任何间距的优势,当你的标签的宽度(在我的情况按钮)改变。

1

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

+0

这是一个很好的教程,但我没有看到任何与问题直接相关的内容。 –

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*/ 

以上constrainsWithVisualFormat基本上都是从左边说右边缘horizo​​ntaly绘制6级金钱的标签,所有与20的宽度,它们之间宽度可变。我没有运行这个代码,但我认为它会起作用(或者至少是接近)。

1

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

这是相同的解决方案,因为我提供的这个问题:

Using Auto Layout to change spaces equally on resizing