更新答:
代替计算框架,你可以使用自动布局做到这一点很容易地。
我已经做了示范项目为您的方案,以下是它看起来像(我已经打开ShowViewFrame)查看:
![enter image description here](https://i.stack.imgur.com/u6I9n.png)
以下是我已经习惯了,使这个约束:设置的视图之间的固定点的间距后
let topLabel = UILabel()
topLabel.translatesAutoresizingMaskIntoConstraints = false
topLabel.text = "TopLabel"
topLabel.textAlignment = NSTextAlignment.Center
let middleLabel = UILabel()
middleLabel.translatesAutoresizingMaskIntoConstraints = false
middleLabel.text = "MiddleLabel"
middleLabel.textAlignment = NSTextAlignment.Center
let bottomtLabel = UILabel()
bottomtLabel.translatesAutoresizingMaskIntoConstraints = false
bottomtLabel.text = "BottomLabel"
bottomtLabel.textAlignment = NSTextAlignment.Center
self.view.addSubview(topLabel)
self.view.addSubview(middleLabel)
self.view.addSubview(bottomtLabel)
//Ideally we should create dictionary for metrics and view to increase the code readability.
//Metrics are used specify width, height, spacing between views.
let metrics = ["spacingBetweenMiddleLabelAndTopLabel" : 25, "spacingBetweenMiddleLabelAndBottomLabel": 25]
//Views we provide the subview for which we are setting the constraints
let views = ["topLabel": topLabel, "middleLabel": middleLabel, "bottomtLabel": bottomtLabel]
//Placing middleLabel at the centre of screen Horizontal
let centreXMiddleLabel:NSLayoutConstraint = NSLayoutConstraint(item: middleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0);
//Placing middleLabel at the centre of screen Vertical
let centreVMiddleLabel:NSLayoutConstraint = NSLayoutConstraint(item: middleLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0);
//Placing topLabel at the centre of screen Horizontal
let centreXTopLabel:NSLayoutConstraint = NSLayoutConstraint(item: topLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0);
//Placing bottomtLabel at the centre of screen Horizontal
let centreXBottomtLabel:NSLayoutConstraint = NSLayoutConstraint(item: bottomtLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0);
//Setting height of 50pts to topLable
let heightTopLabel = NSLayoutConstraint.constraintsWithVisualFormat("V:[topLabel(50)]", options: [], metrics: nil, views: views)
//Setting width of 300pts to topLable
let widthTopLabel = NSLayoutConstraint.constraintsWithVisualFormat("H:[topLabel(300)]", options: [], metrics: nil, views: views)
//Setting height of 100pts to middleLabel
let heightMiddleLabel = NSLayoutConstraint.constraintsWithVisualFormat("V:[middleLabel(100)]", options: [], metrics: nil, views: views)
//Setting width of 150pts to middleLabel
let widthMiddleLabel = NSLayoutConstraint.constraintsWithVisualFormat("H:[middleLabel(150)]", options: [], metrics: nil, views: views)
//Setting height of 50pts to bottomtLabel
let heightBottomLabel = NSLayoutConstraint.constraintsWithVisualFormat("V:[bottomtLabel(50)]", options: [], metrics: nil, views: views)
//Setting width of 300pts to bottomtLabel
let widthBottomLabel = NSLayoutConstraint.constraintsWithVisualFormat("H:[bottomtLabel(300)]", options: [], metrics: nil, views: views)
/*This is important
Here you are setting the Y position of all lables
We are placing top label on top of middleLabel with standand spacing (8 pts) and bottom label below middle label with standard spacing.
*/
let labelYConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:[topLabel]-(spacingBetweenMiddleLabelAndTopLabel)-[middleLabel]-(spacingBetweenMiddleLabelAndBottomLabel)-[bottomtLabel]", options: [], metrics: metrics, views: views)
//If you want to set spacing or height and width with certain priority you can do that
let labelYConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:[topLabel]-([email protected])-[middleLabel]-([email protected])-[bottomtLabel]", options: [], metrics: metrics, views: views)
topLabel.addConstraints(heightTopLabel)
topLabel.addConstraints(widthTopLabel)
middleLabel.addConstraints(heightMiddleLabel)
middleLabel.addConstraints(widthMiddleLabel)
bottomtLabel.addConstraints(heightBottomLabel)
bottomtLabel.addConstraints(widthBottomLabel)
self.view.addConstraint(centreXMiddleLabel)
self.view.addConstraint(centreVMiddleLabel)
self.view.addConstraint(centreXTopLabel)
self.view.addConstraint(centreXBottomtLabel)
self.view.addConstraints(labelYConstraint)
结果:
![enter image description here](https://i.stack.imgur.com/YvKOx.png)
所以你要在屏幕中央(CentreX和CentreY)的Level标签?和其他两个标签下面的标签标签还是什么? –