idhun90
idhun90

Reputation: 41

using CompositionalLayout, and there is a problem that the item disappears for a while when I scroll after calling UIMenu

enter image description here

Hello.

Three sections were constructed using CompositionalLayout, and there is a problem that the item disappears for a while when I scroll after calling UIMenu.

If anyone knows the cause or solution of the problem, please help us.

class ViewController

let collectionView: UICollectionView = {
    let view = UICollectionView(frame: .zero, collectionViewLayout: createCompositionalLayout())
    view.register(CollectionViewCell.self, forCellWithReuseIdentifier: CollectionViewCell.id)
    view.register(CollectionViewHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: CollectionViewHeader.id)
    view.backgroundColor = .systemGroupedBackground
    
    //let layout = UICollectionViewCompositionalLayout { (section, env) -> NSCollectionLayoutSection? in }
    
    func createCompositionalLayout() -> UICollectionViewCompositionalLayout {

        return UICollectionViewCompositionalLayout { (sectionNumber, env) -> NSCollectionLayoutSection? in
            
            switch sectionNumber {
            case 0: return firstSection()
            case 1: return secondSection()
            case 2: return thirdSection()
            default: return firstSection()
            }
        }
    }
    
    
    func firstSection() -> NSCollectionLayoutSection {

        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalHeight(1.0))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)

        item.contentInsets = .init(top: 0, leading: 0, bottom: 11, trailing: 11)

        
        let gropuHeight = NSCollectionLayoutDimension.fractionalWidth(1)
        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.47), heightDimension: .absolute(135))//.fractionalWidth(0.36))
        
        let section = NSCollectionLayoutSection(group: group)
        section.orthogonalScrollingBehavior = .continuous
        
        section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 20, trailing: 0)
        
        section.boundarySupplementaryItems = [
            NSCollectionLayoutBoundarySupplementaryItem(layoutSize: .init(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(44)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .topLeading)
        ]
        

        return section
    }
    
    func secondSection() -> NSCollectionLayoutSection {

        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalWidth(1))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        
        item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 4, trailing: 4)

        let gropuHeight = NSCollectionLayoutDimension.fractionalWidth(0.7)
        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.92), heightDimension: gropuHeight)

        let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 3)
        

        let section = NSCollectionLayoutSection(group: group)
        section.orthogonalScrollingBehavior = .groupPaging
        
        section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 20, trailing: 0)
        
        return section
    }
    
    func thirdSection() -> NSCollectionLayoutSection {

        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(0.5))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        
        item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 2, bottom: 2, trailing: 2)

        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(0.5))

        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: 2)
        group.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 2, bottom: 2, trailing: 2)

        let section = NSCollectionLayoutSection(group: group)
    
        section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 20, trailing: 16)
        
        return section
    }
    
    return view
}()


override func viewDidLoad() {
    super.viewDidLoad()
    
    view.addSubview(collectionView)
    
    collectionView.delegate = self
    collectionView.dataSource = self
    
    collectionView.snp.makeConstraints {
        $0.edges.equalToSuperview()
    }
}

}

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource

func numberOfSections(in collectionView: UICollectionView) -> Int {
    3
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    switch section {
    case 0:
        return 10
    case 1:
        return 10
    case 2:
        return 9
    default:
        return 0
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.id, for: indexPath) as? CollectionViewCell else { return UICollectionViewCell() }

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath)
}

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: CollectionViewHeader.id, for: indexPath) as? CollectionViewHeader else { return UICollectionReusableView() }
    
    header.label.text = "Section \(indexPath.section + 1)"
    
    return header
}

        func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemsAt indexPaths: [IndexPath], point: CGPoint) -> UIContextMenuConfiguration? {

            print(indexPaths)

            return UIContextMenuConfiguration(actionProvider: { suggestedActions in
                   if indexPaths.count == 0 {
                       // Construct an empty-space menu.
                       return UIMenu(children: [
                           UIAction(title: "New Folder") { _ in /* Implement the action. */ }
                       ])
                   }
                   else if indexPaths.count == 1 {
                       // Construct a single-item menu.
                       return UIMenu(children: [
                           UIAction(title: "Copy") { _ in /* Implement the action. */ },
                           UIAction(title: "Delete", attributes: .destructive) { _ in /* Implement the action. */ }
                       ])
                   }
                   else {
                       // Construct a multiple-item menu.
                       return UIMenu(children: [
                           UIAction(title: "New Folder With Selection") { _ in /* Implement the action. */ }
                       ])
                   }
               })
        }

}

Upvotes: 1

Views: 224

Answers (0)

Related Questions