Add subselections to UICollectionViewCell contentView via xib or storyboard

I know there is a message: adding a contentView via nib to the UICollectionViewCell , asking about it, but the answer is incorrect. By dragging a UILabel directly into the xib cell, the label will be added to the cell, not to the contentView:

(lldb) po self.subviews
<__NSArrayM 0x7fa9b1c58a20>(
<UIView: 0x7fa9b1c77540; frame = (0 0; 64 64); gestureRecognizers = <NSArray: 0x7fa9b1c66260>; layer = <CALayer: 0x7fa9b1c352f0>>,
<UILabel: 0x7fa9b1c7a880; frame = (0 15; 64 18); text = 'market share'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa9b1c6f820>>,
<UILabel: 0x7fa9b1c77c30; frame = (0 38; 64 22); text = '17.06'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa9b1c36fe0>>
)


(lldb) po self.contentView.subviews
<__NSArray0 0x7fa9b1d02740>(

)

This is still true for Xcode 7, sample: https://github.com/liuxuan30/autolayoutProblemOniOS9 .

So I asked him again for a good answer.

The behavior is different from UITableViewCell, which will directly display the contentView in xib. I don't know why Apple did this and said in the UICollectionViewCell doc https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewCell_class/ :

, , contentView. .

+4
1

, Apple, , cellView, xib, , , sub.contentView subView.

, sub- xib, contentView, xib UICollectionViewCell xib. ContentView xib, UITableViewCell.

+8

Source: https://habr.com/ru/post/1623190/


All Articles