From e75e275674a9109d7e91c9af1a073a27d406f4c6 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Tue, 28 Apr 2026 23:55:33 +0100 Subject: [PATCH] fixed the colelctionview again --- .../CollectionView/EmojiCollectionView.swift | 9 ++- .../EmojiCollectionViewCell.swift | 13 ++--- .../PlainEmojiCollectionViewCell.swift | 57 ++++++++++--------- 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift index b072066..db1e9a4 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift @@ -102,7 +102,10 @@ struct EmojiCollectionView: UIViewRepresentable { return items.count } - override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + override func collectionView( + _ collectionView: UICollectionView, + cellForItemAt indexPath: IndexPath + ) -> UICollectionViewCell { let cell: PlainEmojiCollectionViewCell switch style { case .plain, .plainWithMenu: @@ -136,9 +139,9 @@ struct EmojiCollectionView: UIViewRepresentable { label.text = emojiName let labelHeight = label.sizeThatFits(CGSize(width: width, height: .infinity)).height - print(width+labelHeight) + print(width+labelHeight+4) - return CGSize(width: width, height: width+labelHeight) + return CGSize(width: width, height: width+labelHeight+4) } override func collectionView( diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift index ee4f692..bcfdfd8 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift @@ -18,14 +18,7 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { label.font = .preferredFont(forTextStyle: .caption1) label.textAlignment = .center label.numberOfLines = 0 - contentView.addSubview(label) label.translatesAutoresizingMaskIntoConstraints = false - - NSLayoutConstraint.activate([ - label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), - label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), - label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor) - ]) } required init?(coder: NSCoder) { @@ -39,8 +32,10 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { override func configure(with: EmojiHoarder, emoji: Emoji) { super.configure(with: with, emoji: emoji) label.text = emoji.UIName - stackView.addArrangedSubview(label) -// setNeedsLayout() + + if label.superview == nil { + stackView.addArrangedSubview(label) + } } override func prepareForReuse() { diff --git a/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift b/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift index 9d94c7b..3d6623e 100644 --- a/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift +++ b/StickerSlack/UIKit/CollectionView/PlainEmojiCollectionViewCell.swift @@ -16,18 +16,13 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - func configureSkeleton() { - spinner.frame = frame - spinner.startAnimating() - self.contentView.addSubview(stackView) - stackView.addArrangedSubview(spinner) + stackView.axis = .vertical + stackView.alignment = .fill + stackView.distribution = .fill + stackView.spacing = 4 + + contentView.addSubview(stackView) stackView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ stackView.topAnchor.constraint(equalTo: contentView.topAnchor), @@ -35,13 +30,26 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell { stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor) ]) - + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func configureSkeleton() { + spinner.startAnimating() + if spinner.superview == nil { + stackView.addArrangedSubview(spinner) + } } func configure(with: EmojiHoarder, emoji: Emoji) { + spinner.stopAnimating() + spinner.removeFromSuperview() let swiftUIView = StickerPreview(sticker: emoji) - if let hostingController { + if let hostingController, + hostingController.view.superview != nil { hostingController.rootView = swiftUIView } else { let hostingController = UIHostingController(rootView: swiftUIView) @@ -49,25 +57,22 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell { self.stackView.addArrangedSubview(hostingController.view) hostingController.view.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate( - [ - hostingController.view.topAnchor.constraint(equalTo: stackView.topAnchor), - hostingController.view.leadingAnchor.constraint(equalTo: stackView.leadingAnchor), - hostingController.view.trailingAnchor.constraint(equalTo: stackView.trailingAnchor), - hostingController.view.heightAnchor.constraint(equalTo: hostingController.view.widthAnchor) - ] - ) + NSLayoutConstraint.activate([ + hostingController.view.heightAnchor.constraint(equalTo: hostingController.view.widthAnchor) + ]) } } override func prepareForReuse() { super.prepareForReuse() - hostingController?.view.removeFromSuperview() - hostingController = nil - if spinner.superview == nil { - configureSkeleton() + stackView.arrangedSubviews.forEach { + stackView.removeArrangedSubview($0) + $0.removeFromSuperview() } - spinner.startAnimating() + + hostingController?.view.removeFromSuperview() + + configureSkeleton() } }