2016-03-03 50 views
0

我是Swift的新手,尝试制作第一个iOS应用。Swift - 在不同的Y位置使用多个UILabel中心

我有UILabels的问题。

做了一个中心(x和y) - 它在框架的中间是完美的。

//UILabel1 (works) 
    Level = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 100)) 
      Level.center = (self.view?.center)! 
      Level.textAlignment = NSTextAlignment.Center 

现在我有2个,我想要在x的中心,但y应该更高,以便他们在屏幕上更高。

我可以将它们居中置于等级。这可行,但如果我尝试更改“y”(在CGRect()中),或者我删除了.center (self.view?.center)!,它就会消失在屏幕上。

GeschwindigkeitUILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 50)) 
    GeschwindigkeitUILabel.center = (self.view?.center)! 

    Regel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 50)) 
    Regel.center = (self.view?.center)! 

怎样才能让GeschwindigkeitUILabel和葱x的中心,但具有较高的y值?

试过self.view.height/2 + 150之类的东西,但是我根本看不到标签。

+0

所以你要在屏幕中央(CentreX和CentreY)的Level标签?和其他两个标签下面的标签标签还是什么? –

回答

1

更新答:

代替计算框架,你可以使用自动布局做到这一点很容易地。

我已经做了示范项目为您的方案,以下是它看起来像(我已经打开ShowViewFrame)查看:

enter image description here

以下是我已经习惯了,使这个约束:设置的视图之间的固定点的间距后

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

+0

谢谢!它代码很多,但它的工作原理就像你说的。:-)帮助很多。谢谢你的“//”行 - 这也解释了很多。 – aignetti

+0

哦,对不起..还有1个问题。我如何改变间距?如果我不想使用8pts的标准间距,但是我想要更多/更少? – aignetti

+0

谢谢!它的工作原理!:-) – aignetti

0

最好的解决方案是使用自动布局,但如果你要坚持使用框架,这里有一些信息。

设置帧中的x和y将被改变中心点覆盖。如果你想实现这个定位,那么我可以在这种情况下给出最好的建议。未经测试,但这可能会给你一个想法。

Level = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 100)) 
      Level.center = (self.view?.center)! 
      Level.textAlignment = NSTextAlignment.Center 

GeschwindigkeitUILabel = UILabel(frame: CGRect(x: 0, y: Level.frame.origin.y+Level.frame.size.height + PADDING, width: 300, height: 50)) 
    GeschwindigkeitUILabel.center = CGPointMake(self.view?.center.x, GeschwindigkeitUILabel.center.y) 

    Regel = UILabel(frame: CGRect(x: 0, y: GeschwindigkeitUILabel.frame.origin.y+GeschwindigkeitUILabel.frame.size.height+ PADDING, width: 300, height: 50)) 
    Regel.center = CGPointMake(self.view?.center.x, Regel.center.y) 
+0

谢谢!我是否必须将它们添加为子视图? 'self.view?.addSubview(等级) self.view?.addSubview(葱) self.view?.addSubview(GeschwindigkeitUILabel)' 而我得到的消息_“可选类型CGFloat的?没有解开的价值,你的意思是使用!或?_ 所以我把它改成'((self.view?center.x)!,' – aignetti

相关问题