为什么我的UILabel对象不会在其父视图中显示?

时间:2019-04-23 18:06:51

标签: ios swift uiview uilabel subview

我有UIView的这个子类,它创建了一个UILabel对象并将其添加为子视图。代码如下。我将UIView对象放在视图控制器的视图上,并将子类设置为我的子类UTIDropDownView。在设备上运行该应用程序时,我可以看到绘制的线条,但是看不到UILabel对象。

有人可以告诉我我在做什么错吗?

import UIKit

class UTIDropDownView: UIView {

    var label = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)

        let labelFrame = CGRect(width: frame.width - frame.height, height: frame.height)

        label = UILabel(frame: labelFrame)

        addSubview(label)

        label.text = "UTIDropDownView"

        label.isHidden = false
        label.isUserInteractionEnabled = true
        label.backgroundColor = UIColor.red

        label.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: frame.height).isActive = true
        NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0).isActive = true

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {

        let labelWidth: CGFloat = frame.width - frame.height
        let triangleBorder: CGFloat = frame.height * 0.1

        let path = UIBezierPath()

        UIColor.darkGray.setStroke()

        path.move(to: CGPoint(x: labelWidth + triangleBorder, y: triangleBorder))
        path.addLine(to: CGPoint(x: frame.width - triangleBorder, y: triangleBorder))
        path.stroke()

    }

}

当我在draw(_ :)中添加以下代码时

print(rect)

我得到以下打印结果:

  

(0.0,0.0,120.0,30.0)

1 个答案:

答案 0 :(得分:0)

您正在覆盖draw(_ rect:),但实际上并未在该函数中绘制标签。您可以在draw方法中调用super.draw(rect),以使视图先绘制其内容,然后再绘制自定义内容。

class UTIDropDownView: UIView {

let label = UILabel()

override init(frame: CGRect) {
    super.init(frame: frame)
    setupLabel()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupLabel()
}

private func setupLabel() {
    let labelFrame = CGRect(width: frame.width - frame.height, height: frame.height)

    label.frame = labelFrame

    addSubview(label)

    label.text = "UTIDropDownView"

    label.isHidden = false
    label.isUserInteractionEnabled = true
    label.backgroundColor = UIColor.red

    label.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: frame.height).isActive = true
    NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0).isActive = true

}

override func draw(_ rect: CGRect) {
    super.draw(rect)

    ...
}

}