我已经使用自动布局在情节提要中设计了视图,但是使用VFL以编程方式实现此功能时遇到了一些问题。
此视图的肖像如下
ios - iOS自动布局通过VFL编程实现-LMLPHP

有三个子视图以某种顺序垂直排列,并且可以通过某种方法更改它们的顺序(即[1,2,3]到[2,3,1])。 view1,view2,view3都具有固定的width = 100和height = 100,view2的中心等于其superview的中心。
更改为横向模式时,三个子视图的水平排列方式与纵向相同,如下所示:
ios - iOS自动布局通过VFL编程实现-LMLPHP

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

最佳答案

由于您无法设置约束,仅使用VFL就可以满足所有要求,因此可以采用以下解决方案:

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)
}

关于ios - iOS自动布局通过VFL编程实现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38170706/

10-08 21:00