简体   繁体   English

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

[英]Unable to center my UILabel over my custom view

I'm creating a tap war game and I added to custom views to my app, a redView and blueView. 我正在创建一个水龙头战争游戏,并将其添加到我的应用程序的自定义视图中,即redView和blueView。 Both views take up half of the screen. 两种视图都占据屏幕的一半。 I'm trying to position a label over each of the two views to keep track of scores (labels are called "topviewLabel" and "bottomViewLabel"), specifically in the center, but the labels appear in the top left corners of their respective views and won't center properly. 我正在尝试在两个视图的每个视图上放置一个标签,以跟踪得分(标签分别称为“ topviewLabel”和“ bottomViewLabel”),尤其是在中间,但是标签出现在它们各自视图的左上角并且无法正确居中。 I create the views and set their constraints programmatically and then add labels to the redView or blueView, I'm doing this all programmatically. 我创建视图并以编程方式设置其约束,然后将标签添加到redView或blueView中,这是通过编程方式完成的。 I'll leave the code below, let me know what I'm doing wrong. 我将在下面的代码,让我知道我做错了。

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)

}

A UIView doesn't have its correct frame size right after adding constraints, you need to add: 添加约束后, UIView框大小不正确,您需要添加:

blueView.setNeedsLayout()
blueView.layoutIfNeeded()

Now blueView 's frame is set. 现在, blueView的框架已设置。

As paulvs said, in viewDidLoad your subviews do not have their frames set yet. 正如paulvs所说,在viewDidLoad中,您的子视图尚未设置框架。 So when, for example, you try to set your label frame 因此,例如,当您尝试设置标签框架时

topViewLabel.frame = CGRect(x: redView.frame.size.width / 2, y: redView.frame.size.height / 2, width: 80, height: 80)

redview.frame has width and height 0. redview.frame的宽度和高度为0。

These parameters will be calculated properly only in layoutSubviews. 仅在layoutSubviews中将正确计算这些参数。

So you can either set you labels frames in viewDidLayoutSubviews() method or (which I would do) use constraints for it. 因此,您可以在viewDidLayoutSubviews()方法中设置框架标签,或者(我会这样做)使用约束。 Something like 就像是

let topViewLabelConstraints: [NSLayoutConstraint] = [
    NSLayoutConstraint(item: topViewLabel, attribute: .centerX, relatedBy: .equal, toItem: redView, attribute: .centerX, multiplier: 1, constant: 0),
    NSLayoutConstraint(item: topViewLabel, attribute: .centerY, relatedBy: .equal, toItem: redView, attribute: .centerY, multiplier: 1, constant: 0)
]
redView.addConstraints(topViewLabelConstraints)

should do the trick. 应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM