[英]UILabel is not visible on dynamically created UIView
I'm attempting to create a custom view programmatically to show an error message for an error condition. 我正在尝试以编程方式创建自定义视图,以显示针对错误情况的错误消息。 The following is what the ViewController looks like: 以下是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
}
I can only see a red coloured rectangle, which means that only the errorView is visible. 我只能看到一个红色的矩形,这意味着只有errorView是可见的。 What am I doing wrong? 我究竟做错了什么?
我认为您的UILabel需要框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.