简体   繁体   中英

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:

// 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. What am I doing wrong?

我认为您的UILabel需要框架。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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