无法将我的UILabel置于自定义视图的中心

时间:2017-04-20 23:14:25

标签: ios uiview swift3 uilabel constraints

我正在创建一个点击战游戏,我添加到我的应用程序的自定义视图,一个redView和blueView。两个视图都占据了屏幕的一半。我正在尝试在两个视图中的每个视图上放置一个标签以跟踪分数(标签被称为“topviewLabel”和“bottomViewLabel”),特别是在中心,但标签出现在各自视图的左上角并且不会正确居中。我以编程方式创建视图并设置其约束,然后将标签添加到redView或blueView,我正在以编程方式执行此操作。我将保留下面的代码,让我知道我做错了什么。

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let redView = UIView()
    redView.backgroundColor = UIColor(red: 206/255, green: 0/255, blue: 0/255, alpha: 1)
    redView.isMultipleTouchEnabled = true
    redView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(redView)

    let redviewConstraints: [NSLayoutConstraint] = [
        NSLayoutConstraint(item: redView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: redView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: redView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: redView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: view.frame.height / 2)
    ]

    view.addConstraints(redviewConstraints)

    let blueView = UIView()
    blueView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 99/255, alpha: 1)
    blueView.isMultipleTouchEnabled = true
    blueView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(blueView)

    let blueViewConstraints: [NSLayoutConstraint] = [
        NSLayoutConstraint(item: blueView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: blueView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: blueView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0),
        NSLayoutConstraint(item: blueView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: view.frame.height / 2)
    ]

    view.addConstraints(blueViewConstraints)


    let topViewLabel = UILabel()
    topViewLabel.frame = CGRect(x: redView.frame.size.width / 2, y: redView.frame.size.height / 2, width: 80, height: 80)
    topViewLabel.text = "30"
    topViewLabel.textAlignment = .center
    topViewLabel.font.withSize(30)
    topViewLabel.textColor = .white
    topViewLabel.backgroundColor = UIColor.blue
    topViewLabel.center = redView.center
    redView.addSubview(topViewLabel)
    redView.bringSubview(toFront: topViewLabel)

    let bottomViewLabel = UILabel()
    bottomViewLabel.frame = CGRect(x: blueView.frame.width / 2, y: blueView.frame.height / 2, width: 80, height: 80)
    bottomViewLabel.center = blueView.center
    bottomViewLabel.text = "20"
    bottomViewLabel.backgroundColor = UIColor.red
    blueView.addSubview(bottomViewLabel)
    blueView.bringSubview(toFront: bottomViewLabel)

}

2 个答案:

答案 0 :(得分:0)

添加约束后,UIView没有正确的帧大小,您需要添加:

blueView.setNeedsLayout()
blueView.layoutIfNeeded()

现在设置了blueView的框架。

答案 1 :(得分:0)

正如 paulvs 所说,在viewDidLoad中,您的子视图尚未设置其框架。因此,例如,当您尝试设置标签框架时

x:
[[0 1]
 [2 3]
 [4 5]
 [6 7]],
y:
[[0 1]
 [2 3]]

for loop:
row 0: [0 1] + [0 1]
row 1: [0 1] + [2 3]
row 2: [2 3] + [0 1]
row 3: [2 3] + [2 3]
row 4: [4 5] + [0 1]
row 5: [4 5] + [2 3]
row 6: [6 7] + [0 1]
row 7: [6 7] + [2 3]

z:
[[  0.   2.]
 [  2.   4.]
 [  2.   4.]
 [  4.   6.]
 [  4.   6.]
 [  6.   8.]
 [  6.   8.]
 [  8.  10.]]

redview.frame的宽度和高度为0。

这些参数只能在layoutSubviews中正确计算。

因此,您可以在viewDidLayoutSubviews()方法中设置标签帧 或者(我会这样做)使用约束。像

这样的东西
topViewLabel.frame = CGRect(x: redView.frame.size.width / 2, y: redView.frame.size.height / 2, width: 80, height: 80)

应该这样做。

相关问题