Thursday, April 4, 2024
HomeiOS Developmentios - Dynamic width horizontal Assortment view utilizing compositional structure

ios – Dynamic width horizontal Assortment view utilizing compositional structure


I’m attempting to create a group view that has a easy cell with textual content, which signifies that the width and peak of the cells must be dynamic. Because it ought to be capable of be shorter/longer and the variety of traces is n (Textual content comes from the service). So long as The cells width is just not greater than the gathering it self, there isn’t a downside. But when a protracted textual content must be introduced it crashes.

It could solely occur utilizing .estimate as dimension, it wont if the cell take all of the attainable area

That is how the implementation appears to be like like

Error:

Thread 1: "<UICollectionViewCompositionalLayout: 0x105b08470> bumped into an error when computing the structure for part at index 0 in container <_UICollectionLayoutContainer: 0x600001756440 contentSize={353, 500}; contentInsets={0, 0, 0, 0}}>. Container calculated by making use of insets ({0, 0, 0, 0}) from sectionInsetsReference "safeArea" to assortment view body {{0, 0}, {353, 500}}.nError: NSCollectionLayoutItem created with invalid mixture of spacing and dimension specified. This group can not match even a single merchandise. Examine the spacing and dimension of the gadgets on this group and be sure that they match into the group when its efficient dimension is {333, 480}.nGroup: <NSCollectionLayoutGroup 0x6000035082c0 dimension={.containerWidthFactor(1), .estimated(480)}>nt layoutDirection: .horizontalnt interItemSpacing=.fastened(4)nt subitems=ntt <NSCollectionLayoutItem 0x600002620660 dimension={.estimated(32), .estimated(120)}>"

The collectionView is inside a UIView that works as wrapper and the view is inside a StackView like this:

        stackView.addArrangedSubview(label)
        stackView.addArrangedSubview(collectionViewWrapper)
        stackView.backgroundColor = .white
        collectionViewWrapper.translatesAutoresizingMaskIntoConstraints = false
        let actionGruopViewHeight = collectionViewWrapper.heightAnchor.constraint(equalToConstant: 200)
        NSLayoutConstraint.activate([
            stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
            stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
            stackView.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: -20),
            collectionViewWrapper.widthAnchor.constraint(equalTo: stackView.widthAnchor),
            actionGruopViewHeight
        ])
        view.backgroundColor = .purple

        collectionViewWrapper.didLayoutAction =  {
            actionGruopViewHeight.fixed = collectionViewWrapper.collectionView.contentSize.peak
        }

CollectionView wrapper:

        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(ActionCell.self, forCellWithReuseIdentifier: ActionCell.reuseIdentifier)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.backgroundColor = .inexperienced
        addSubview(collectionView)
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: topAnchor),
            collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
            collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
            collectionView.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])

CollectionViewLayout:

    non-public lazy var structure = UICollectionViewCompositionalLayout { [weak self] _, _ in
        let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(32), heightDimension: .estimated(120))
        let merchandise = NSCollectionLayoutItem(layoutSize: itemSize)
        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(480))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
        group.interItemSpacing = .fastened(4)
        let part = NSCollectionLayoutSection(group: group)
        part.contentInsets = .init(high: 10, main: 10, backside: 10, trailing: 10)
        part.interGroupSpacing = 4
        return part
    }

Cell:

    non-public lazy var textLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.numberOfLines = 0
        return textLabel
    }()

    override init(body: CGRect) {
        tremendous.init(body: body)
        contentView.addSubview(textLabel)
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = .grey
        NSLayoutConstraint.activate([
            textLabel.topAnchor.constraint(equalTo: contentView.topAnchor),
            textLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            textLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            textLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        ])
    }

Did I do one thing flawed? is there a repair/workaround to keep away from the crash?

Discover that it will be simple to breed with accessibility font sizes, since often it will want 2 traces



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments