使用可视格式语言的UIButtons之间的相等间距

我有一堆UIButtons,我想在容器视图中均匀分隔,现在我对间距有这个约束:

someView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(“H:| – (> = 0) – [M] – (> = 0) – [T] – (> = 0) – [W] – (> = 0) – [ T] – (> = 0) – [F] – (> = 0) – [S] – (> = 0) – [S] – (> = 0) – |“,选项:NSLayoutFormatOptions.AlignAllCenterY,指标:零,意见:buttonsArray))

但是这会使按钮看起来像这样:

1

问题是我想要的间距是这样计算的:

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

someView.frame.height * 0.6是按钮的边长。 我不知道该怎么做。

这是一个简单的代码,它确实在视图中按钮之间分配空格,我希望这可以帮助你找出你的用例,

  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) 

而且,结果如下,

在此处输入图像描述