Liumx31
Liumx31

Reputation: 1210

Equal spacing between UIButtons using visual format language

I have a bunch of UIButtons that I want to space out evenly in a container view, right now I have this constraint for spacing:

someView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=0)-[M]-(>=0)-[T]-(>=0)-[W]-(>=0)-[T]-(>=0)-[F]-(>=0)-[S]-(>=0)-[S]-(>=0)-|", options: NSLayoutFormatOptions.AlignAllCenterY, metrics: nil, views: buttonsArray))

However this makes the buttons look like this:

1

The problem is what I want for spacing is calculated in this way:

spacing = (someView.frame.width - (someView.frame.height * 0.6) * 7) / 8

someView.frame.height * 0.6 is the side length of the buttons. I am not sure what to do.

Upvotes: 0

Views: 575

Answers (1)

Sandeep
Sandeep

Reputation: 21134

Here is a simple code which does exactly distribute the spaces between buttons in a view, I hope this will help you figure out for your use case,

    let containerView = UIView(frame: CGRect.zero)
    containerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(containerView)

    let M = UIButton(type: .System)
    M.translatesAutoresizingMaskIntoConstraints = false
    M.backgroundColor = UIColor.lightGrayColor()
    M.setTitle("M", forState: .Normal)
    containerView.addSubview(M)

    let T = UIButton(type: .System)
    T.translatesAutoresizingMaskIntoConstraints = false
    T.setTitle("T", forState: .Normal)
    T.backgroundColor = UIColor.lightGrayColor()
    containerView.addSubview(T)

    let W = UIButton(type: .System)
    W.translatesAutoresizingMaskIntoConstraints = false
    W.setTitle("W", forState: .Normal)
    W.backgroundColor = UIColor.lightGrayColor()
    containerView.addSubview(W)

    let Th = UIButton(type: .System)
    Th.translatesAutoresizingMaskIntoConstraints = false
    Th.setTitle("T", forState: .Normal)
    Th.backgroundColor = UIColor.lightGrayColor()
    containerView.addSubview(Th)

    let F = UIButton(type: .System)
    F.translatesAutoresizingMaskIntoConstraints = false
    F.setTitle("F", forState: .Normal)
    F.backgroundColor = UIColor.lightGrayColor()
    containerView.addSubview(F)

    let S = UIButton(type: .System)
    S.translatesAutoresizingMaskIntoConstraints = false
    S.setTitle("S", forState: .Normal)
    S.backgroundColor = UIColor.lightGrayColor()
    containerView.addSubview(S)

    let Su = UIButton(type: .System)
    Su.translatesAutoresizingMaskIntoConstraints = false
    Su.setTitle("Su", forState: .Normal)
    Su.backgroundColor = UIColor.lightGrayColor()
    containerView.addSubview(Su)

    let views = [
        "M": M,
        "T": T,
        "W": W,
        "Th":Th,
        "F": F,
        "S": S,
        "Su": Su
    ]

    let horizontalSpacing = 20
    let cornerMargin = 30

    let metrics = [
        "horizontalSpacing": horizontalSpacing,
        "cornerMargin": cornerMargin
    ]

    views.values.forEach { view in
        view.clipsToBounds = true
        view.layer.cornerRadius = 10
    }


    let verticalCenter = NSLayoutConstraint(item: containerView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0)
    let horizontalCenter = NSLayoutConstraint(item: containerView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 0)

    view.addConstraint(verticalCenter)
    view.addConstraint(horizontalCenter)


    let horizontalFormat = "H:|-(==cornerMargin)-[M]-horizontalSpacing-[T]-horizontalSpacing-[W]-horizontalSpacing-[Th]-horizontalSpacing-[F]-horizontalSpacing-[S]-horizontalSpacing-[Su]-(==cornerMargin)-|"
    let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(horizontalFormat, options: .AlignAllCenterY, metrics: metrics, views: views)
    view.addConstraints(horizontalConstraints)


    let verticalFormat = "V:|-[M]-|"
    let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(verticalFormat, options: .AlignAllCenterY, metrics: metrics, views: views)
    view.addConstraints(verticalConstraints)

And, here is the result,

enter image description here

Upvotes: 1

Related Questions