繁体   English   中英

UILabel在动态创建的UIView上不可见

[英]UILabel is not visible on dynamically created UIView

我正在尝试以编程方式创建自定义视图,以显示针对错误情况的错误消息。 以下是ViewController的外观:

// init
var titleParagraphStyle = NSMutableParagraphStyle()
titleParagraphStyle.lineSpacing = 40.0
titleParagraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping

let titleString = NSMutableAttributedString(
string: "Oops! Error.",
attributes: [ // Some attributes
    ]
)

var errorView = UIView()
errorView.frame = self.view.bounds
errorView.backgroundColor = UIColor.redColor()

var errorLabel = UILabel()
errorLabel.textAlignment = .Center
errorLabel.attributedText = titleString
errorLabel.lineBreakMode = .ByWordWrapping
errorLabel.backgroundColor = UIColor.blueColor()
errorLabel.textColor = UIColor.greenColor()

errorView.addSubview(errorLabel)
self.errorView = errorView

// usage
if(errorCondition) {
    self.view = self.errorView
}

我只能看到一个红色的矩形,这意味着只有errorView是可见的。 我究竟做错了什么?

我认为您的UILabel需要框架。

暂无
暂无

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

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