Following phenomena happens when using Xcode 7 beta 5 and Swift 2:
When using a custom UICollectionViewCell that is created in the storyboard, the cell's subviews are not added to the cell's contentView
. Thus the cell remains blank on runtime.
If I however create a custom cell class for the cell and then programmatically add the subviews to the contentView and set their frame
the cell's content is displayed:
class Cell : UITableViewCell {
@IBOutlet weak var label: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
NSLog("subiews.count=%d", contentView.subviews.count) // prints "0"
contentView.subviews.count
contentView.addSubview(label)
label.frame = CGRect(x: 0, y: 0, width: 200, height: 21)
}
}
Again, without manually adding the label (that has been added in the storyboard!) and setting its frame, it would not be visible at runtime! In the storyboard the label is a subview of the content view. At run time it is not.
I cannot observe this behavior in latest Xcode 6 with Swift 1.2.
Can somebody confirm this silly behavior? And maybe provide an easier workaround?
Edit:
Luckily view constraints on the cell's subviews are applied after these views have been added programmatically to contentView
. Thus at least manually setting their frames is not necessary.