iOS自动布局以VFL编程方式实现

时间:2016-07-03 14:02:08

标签: ios

我使用自动布局在故事板中设计了我的视图,但是我在使用VFL以编程方式实现此问题时遇到了一些问题。 这个视图在肖像中看起来像这样 enter image description here

有三个子视图按某种顺序垂直排列,在某种方法中,我们可以改变它们的顺序(即[1,2,3]到[2,3,1])。 view1,view2,view3都有固定宽度= 100和高度= 100,view2的中心等于其superview的中心。 当更改为横向模式时,三个子视图水平布局的顺序与它们在纵向中的顺序相同,如下所示: enter image description here

希望有人能帮助我实现这一点,感谢您的帮助!在这里你可以下载我的示例项目,以防我没有清楚地描述我的问题! https://github.com/SomnusLee1988/SamplePorject

1 个答案:

答案 0 :(得分:1)

因为您无法设置VSP所需的约束,这可能是一个解决方案:

var view1: UIView!
var view2: UIView!
var view3: UIView!

var variableConstraints = [NSLayoutConstraint]()

let metrics = ["width": 100, "height": 100]
var views: [String: AnyObject]!

override func viewDidLoad() {
    super.viewDidLoad()

    view1 = UIView()
    view1.backgroundColor = UIColor.redColor()
    view1.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(view1)

    view2 = UIView()
    view2.backgroundColor = UIColor.blueColor()
    view2.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(view2)

    view3 = UIView()
    view3.backgroundColor = UIColor.greenColor()
    view3.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(view3)

    views = ["v1": view1, "v2": view2, "v3": view3]

    setupFixedConstraints()
    setupVariableConstraintsForSize(nil)
}

private func setupFixedConstraints() {
    // width and height
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("[v2(width)]", options: [], metrics: metrics, views: views))
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[v2(height)]", options: [], metrics: metrics, views: views))

    // center x and center y
    NSLayoutConstraint(item: view2, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: view2, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0).active = true
}

private func setupVariableConstraintsForSize(size: CGSize?) {
    // remove existing constraints
    NSLayoutConstraint.deactivateConstraints(variableConstraints)
    variableConstraints.removeAll()

    let size = size ?? view.frame.size

    if size.width > size.height {
        // landscape
        variableConstraints += NSLayoutConstraint.constraintsWithVisualFormat("[v1(==v2)][v2][v3(==v2)]", options: [.AlignAllTop, .AlignAllBottom], metrics: metrics, views: views)
    } else {
        // portrait
        variableConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:[v1(==v2)][v2][v3(==v2)]", options: [.AlignAllLeading, .AlignAllTrailing], metrics: metrics, views: views)
    }

    // activate constraints
    NSLayoutConstraint.activateConstraints(variableConstraints)
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    setupVariableConstraintsForSize(size)
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}