fixed the colelctionview again

This commit is contained in:
neon443 committed 2026-04-28 23:55:44 +01:00
1 parent 2ca8569258
commit e75e275674
3 files changed
+36 -33

No files matched your search

@@ -102,7 +102,10 @@ struct EmojiCollectionView: UIViewRepresentable {
return items.count return items.count
} }
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { override func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
let cell: PlainEmojiCollectionViewCell let cell: PlainEmojiCollectionViewCell
switch style { switch style {
case .plain, .plainWithMenu: case .plain, .plainWithMenu:
@@ -136,9 +139,9 @@ struct EmojiCollectionView: UIViewRepresentable {
label.text = emojiName label.text = emojiName
let labelHeight = label.sizeThatFits(CGSize(width: width, height: .infinity)).height 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( override func collectionView(
@@ -18,14 +18,7 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
label.font = .preferredFont(forTextStyle: .caption1) label.font = .preferredFont(forTextStyle: .caption1)
label.textAlignment = .center label.textAlignment = .center
label.numberOfLines = 0 label.numberOfLines = 0
contentView.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false 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) { required init?(coder: NSCoder) {
@@ -39,8 +32,10 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell {
override func configure(with: EmojiHoarder, emoji: Emoji) { override func configure(with: EmojiHoarder, emoji: Emoji) {
super.configure(with: with, emoji: emoji) super.configure(with: with, emoji: emoji)
label.text = emoji.UIName label.text = emoji.UIName
if label.superview == nil {
stackView.addArrangedSubview(label) stackView.addArrangedSubview(label)
// setNeedsLayout() }
} }
override func prepareForReuse() { override func prepareForReuse() {
@@ -16,18 +16,13 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) { override init(frame: CGRect) {
super.init(frame: frame) super.init(frame: frame)
}
required init?(coder: NSCoder) { stackView.axis = .vertical
fatalError("init(coder:) has not been implemented") stackView.alignment = .fill
} stackView.distribution = .fill
stackView.spacing = 4
func configureSkeleton() {
spinner.frame = frame
spinner.startAnimating()
self.contentView.addSubview(stackView)
stackView.addArrangedSubview(spinner)
contentView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: contentView.topAnchor), stackView.topAnchor.constraint(equalTo: contentView.topAnchor),
@@ -35,13 +30,26 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor) 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) { func configure(with: EmojiHoarder, emoji: Emoji) {
spinner.stopAnimating()
spinner.removeFromSuperview()
let swiftUIView = StickerPreview(sticker: emoji) let swiftUIView = StickerPreview(sticker: emoji)
if let hostingController { if let hostingController,
hostingController.view.superview != nil {
hostingController.rootView = swiftUIView hostingController.rootView = swiftUIView
} else { } else {
let hostingController = UIHostingController(rootView: swiftUIView) let hostingController = UIHostingController(rootView: swiftUIView)
@@ -49,25 +57,22 @@ class PlainEmojiCollectionViewCell: UICollectionViewCell {
self.stackView.addArrangedSubview(hostingController.view) self.stackView.addArrangedSubview(hostingController.view)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false hostingController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate( 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) hostingController.view.heightAnchor.constraint(equalTo: hostingController.view.widthAnchor)
] ])
)
} }
} }
override func prepareForReuse() { override func prepareForReuse() {
super.prepareForReuse() super.prepareForReuse()
stackView.arrangedSubviews.forEach {
stackView.removeArrangedSubview($0)
$0.removeFromSuperview()
}
hostingController?.view.removeFromSuperview() hostingController?.view.removeFromSuperview()
hostingController = nil
if spinner.superview == nil {
configureSkeleton() configureSkeleton()
} }
spinner.startAnimating()
}
} }
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.