[背景]
一个UITableViewCell中的contentView中的UILabel
在init函数中初始化属性,但是没有设置text内容:
let timestampLabelTextColor = UIColor.whiteColor() let timestampBackgroundColor = UIColor(white: 0.734, alpha: 1.0) let timestampLabel = UILabel(frame: CGRectMake(0, LabelPadding, UIScreen.mainScreen().bounds.size.width, TimeStampLabelHeight)) //timestampLabel.text = "" timestampLabel.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] timestampLabel.textColor = timestampLabelTextColor timestampLabel.backgroundColor = timestampBackgroundColor timestampLabel.textAlignment = NSTextAlignment.Center timestampLabel.font = UIFont.boldSystemFontOfSize(TimeStampLabelFont) //timestampLabel.center.x = 350 timestampLabel.numberOfLines = 1 timestampLabel.sizeToFit() self.timestampLabel = timestampLabel print("timestampLabel.frame=\(timestampLabel.frame)") self.contentView.addSubview(timestampLabel) self.contentView.bringSubviewToFront(timestampLabel)
然后在后续的函数
configureCell
中去设置内容:
override func layoutSubviews() { super.layoutSubviews() print("after layoutSubviews, timestampLabel.frame=\(timestampLabel?.frame)") } configureCell{ self.timestampLabel?.text = "昨天 22:08" timestampLabel?.setNeedsLayout() print("self.timestampLabel?.text=\(self.timestampLabel?.text)") }
但是:
after layoutSubviews, timestampLabel.frame=Optional((0.0, 5.0, 55.0, 0.0))
并且,里面的label中text中的文字内容,也不显示。
[解决过程]
1.搜:
swift UILabel autoresizingMask
swift UILabel autoresizingMask not work
swift UITableViewCell contentView not show
参考:
才知道:
指的是:控件相对于父视图坐标值
而不是控件自己的内部的大小
后来是:
init时设置text的值
就可以了。。。