iOS自动布局并重新排列屏幕上的视图

时间:2016-02-20 17:57:42

标签: ios xcode uiview autolayout constraints

我尝试使用AutoLayout在屏幕上安排UIView。 我得到一个包含起点,宽度和高度的视图列表。然后我需要创建表示具有约束的类似排列的约束,但是所有视图都需要" snapped"对彼此。他们需要填补"所有的白色空间。

例如: 如果我有坐标视图:

View 1 : (0,0,300,200)
View 2 : (400,10,300,150)
View 3 : (10,300,200,200)
View 4 : (250,300,400,200)
View 5 : (5,600,400,200) 
View 6 : (506,600,200,200)

我想得到类似的东西:

enter image description here

注意:它确实需要使用约束和自动布局。

有没有一种简单的方法可以做到这一点我想念它?

Marko

1 个答案:

答案 0 :(得分:0)

由于您在点/像素上定义了所需的布局,因此预期结果并不十分清晰,您应该以百分比/相对值进行。

无论如何,你可以这样做:

view1.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view1]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view1(>=200)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

view2.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view1(==400)][view2]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view2(>=150)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view1(==view2)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

view3.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view3]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view1][view3(>=200)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

view4.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view3(==250)][view4]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view2][view4(>=200)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view3(==view4)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

view5.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view5]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view3][view5(>=200)]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

view6.translatesAutoresizingMaskIntoConstraints = false
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view5(==506)][view6]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view4][view6(>=200)]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view5(==view6)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))

如果你的目标是iOS 9.x +,你也可以堆叠视图+约束;它将为您节省一些编码。