根据子视图计数在超级视图上定位子视图

时间:2016-03-14 14:56:33

标签: ios swift uiview autolayout alignment

子视图需要根据子视图计数从超级视图的中心放置。 如果子视图计数为奇数,则中间视图将以超视图为中心,剩余部分将相对于项目间距放置。 enter image description here

如果子视图计数是偶数,则子视图将放置偏移量。 enter image description here

是否有任何通用的解决方案可以在没有太多条件和使用autolayout的计算的情况下解决这个问题?

所有子视图的大小相同

2 个答案:

答案 0 :(得分:1)

解决方案1:UIStackView

4 subviews

    let totalSubviews = 4
    let stackView = UIStackView()
    view.addSubview(stackView)
    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.axis = .Horizontal
    stackView.distribution = .EqualSpacing
    stackView.alignment = .Center
    stackView.spacing = 10
    stackView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor, constant: 0).active = true
    stackView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor, constant: 0).active = true
    stackView.backgroundColor = UIColor.greenColor()

    for _ in 0 ..< totalSubviews {
        let subview = UIView()
        stackView.addArrangedSubview(subview)
        subview.translatesAutoresizingMaskIntoConstraints = false
        subview.backgroundColor = UIColor.redColor()
        subview.heightAnchor.constraintEqualToConstant(50).active = true
        subview.widthAnchor.constraintEqualToConstant(50).active = true
    }

解决方案2:自动布局

您可以使用AutoLayout一般性地执行此操作。

totalSubviews = 4

4 subviews

totalSubviews = 5

5 subviews

<强>代码

    let totalSubviews = 4
    let spacing = CGFloat(20)
    var subviews = [UIView]()

    var previousSubview: UIView?
    for i in 0 ..< totalSubviews {
        let subview = UIView()
        subviews.append(subview)
        view.addSubview(subview)
        subview.translatesAutoresizingMaskIntoConstraints = false
        subview.backgroundColor = UIColor.redColor()

        view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: spacing))

        if i == 0 {
            view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1, constant: spacing))
        }
        else if i < totalSubviews {
            view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Left, relatedBy: .Equal, toItem: previousSubview, attribute: .Right, multiplier: 1, constant: spacing))
        }
        if i == totalSubviews - 1 {
            view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1, constant: -spacing))
        }

        if i != 0 {
            view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Width, relatedBy: .Equal, toItem: previousSubview!, attribute: .Width, multiplier: 1, constant: 0))
        }


        view.addConstraint(NSLayoutConstraint(item: subview, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 100))

        previousSubview = subview
    }

答案 1 :(得分:0)

您必须在主视图中再观看1个视图,其中包含您的奇数或偶数subviews。然后你需要在它们之间设置Equal width和Equal height约束。因此,根据您subviews的数量和屏幕尺寸,它们都将自动定位。