簡體   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