@@ -45,8 +45,15 @@ final class EmojiPickerView: UIView {
4545 collectionView. contentInset = UIEdgeInsets ( top: 0 , left: 16 , bottom: 0 , right: 16 )
4646 collectionView. backgroundColor = . clear
4747 collectionView. translatesAutoresizingMaskIntoConstraints = false
48- collectionView. register ( EmojiCollectionViewCell . self, forCellWithReuseIdentifier: EmojiCollectionViewCell . identifier)
49- collectionView. register ( EmojiCollectionViewHeader . self, forSupplementaryViewOfKind: UICollectionView . elementKindSectionHeader, withReuseIdentifier: EmojiCollectionViewHeader . identifier)
48+ collectionView. register (
49+ EmojiCollectionViewCell . self,
50+ forCellWithReuseIdentifier: EmojiCollectionViewCell . identifier
51+ )
52+ collectionView. register (
53+ EmojiCollectionViewHeader . self,
54+ forSupplementaryViewOfKind: UICollectionView . elementKindSectionHeader,
55+ withReuseIdentifier: EmojiCollectionViewHeader . identifier
56+ )
5057 return collectionView
5158 } ( )
5259
@@ -68,12 +75,23 @@ final class EmojiPickerView: UIView {
6875 } ( )
6976
7077 private var categoryViews = [ TouchableEmojiCategoryView] ( )
78+ private var selectedCategoryIndex : Int = . zero
7179
7280 /// Describes height for `categoriesStackView`.
7381 ///
74- /// - Note: The number `0.13` was taken based on the proportion of this element to the width of the EmojiPicker on MacOS.
75- private var categoriesStackViewHeight : CGFloat {
76- return bounds. width * 0.13
82+ /// - Note: The number `0.13` was taken based on the proportion of this element to the width of the EmojiPicker on macOS.
83+ private var categoriesStackViewHeight : CGFloat { bounds. width * 0.13 }
84+ private var categoriesStackHeightConstraint : NSLayoutConstraint ?
85+
86+ // MARK: - Init
87+ override init ( frame: CGRect ) {
88+ super. init ( frame: . zero)
89+ setupView ( )
90+ }
91+
92+ @available ( * , unavailable, message: " init(coder:) has not been implemented " )
93+ required init ? ( coder: NSCoder ) {
94+ fatalError ( " init(coder:) has not been implemented " )
7795 }
7896
7997 // MARK: - Override
@@ -82,27 +100,31 @@ final class EmojiPickerView: UIView {
82100 super. draw ( rect)
83101
84102 setupCategoryViews ( )
85- setupView ( )
86103 }
87104
88105 /// Passes the index of the selected category to all categoryViews to update the state.
89106 ///
90107 /// - Parameter categoryIndex: Selected category index.
91108 func updateSelectedCategoryIcon( with categoryIndex: Int ) {
109+ selectedCategoryIndex = categoryIndex
92110 categoryViews. forEach {
93111 $0. updateCategoryViewState ( selectedCategoryIndex: categoryIndex)
94112 }
95113 }
96114
97115 // MARK: - Private Methods
98-
99116 private func setupView( ) {
100117 backgroundColor = . popoverBackgroundColor
101-
118+
102119 addSubview ( collectionView)
103120 addSubview ( categoriesStackView)
104121 addSubview ( separatorView)
105122
123+ let categoriesStackHeightConstraint = categoriesStackView. heightAnchor. constraint (
124+ equalToConstant: categoriesStackViewHeight
125+ )
126+ self . categoriesStackHeightConstraint = categoriesStackHeightConstraint
127+
106128 NSLayoutConstraint . activate ( [
107129 collectionView. leadingAnchor. constraint ( equalTo: leadingAnchor) ,
108130 collectionView. trailingAnchor. constraint ( equalTo: trailingAnchor) ,
@@ -112,7 +134,7 @@ final class EmojiPickerView: UIView {
112134 categoriesStackView. leadingAnchor. constraint ( equalTo: leadingAnchor, constant: 16 ) ,
113135 categoriesStackView. trailingAnchor. constraint ( equalTo: trailingAnchor, constant: - 16 ) ,
114136 categoriesStackView. bottomAnchor. constraint ( equalTo: bottomAnchor, constant: - safeAreaInsets. bottom) ,
115- categoriesStackView . heightAnchor . constraint ( equalToConstant : categoriesStackViewHeight ) ,
137+ categoriesStackHeightConstraint ,
116138
117139 separatorView. leadingAnchor. constraint ( equalTo: leadingAnchor) ,
118140 separatorView. trailingAnchor. constraint ( equalTo: trailingAnchor) ,
@@ -122,6 +144,10 @@ final class EmojiPickerView: UIView {
122144 }
123145
124146 private func setupCategoryViews( ) {
147+ categoriesStackHeightConstraint? . constant = categoriesStackViewHeight
148+ categoryViews = [ ]
149+ categoriesStackView. subviews. forEach { $0. removeFromSuperview ( ) }
150+
125151 var index = 0
126152 for type in CategoryType . allCases {
127153 let categoryView = TouchableEmojiCategoryView (
@@ -132,7 +158,7 @@ final class EmojiPickerView: UIView {
132158 )
133159
134160 /// We need to set _selected_ state for the first category (default at the start).
135- categoryView. updateCategoryViewState ( selectedCategoryIndex: 0 )
161+ categoryView. updateCategoryViewState ( selectedCategoryIndex: selectedCategoryIndex )
136162 categoryViews. append ( categoryView)
137163 categoriesStackView. addArrangedSubview ( categoryView)
138164 index += 1
0 commit comments