Archive/위젯 캘린더(project-ios)

[iOS] collectionViewCell 사이에 공백없애기

lgvv 2022. 5. 2. 02:40

collectionViewCell 사이에 공백없애기

 

✅ collectionViewCell 사이에 공백없애기

 

아래의 코드를 선언하면 저렇게 빈 공간이 생기게 된다.

   var calendarList: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.register(CalendarCell.self, forCellWithReuseIdentifier: CalendarCell.identifier)
        
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = .red
        
        return collectionView
    }()

 

빈 공간이 생김..

✅ 빈 공간을 없애려면?

   var calendarList: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0 // 상하간격
        layout.minimumInteritemSpacing = 0 // 좌우간격
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.register(CalendarCell.self, forCellWithReuseIdentifier: CalendarCell.identifier)
        
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = .red
        
        return collectionView
    }()

빈 공간을 없앱니다.

 

 

✅ 레이아웃

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // width는 무조건 7개 들어가야한다.
        let widht = view.safeAreaLayoutGuide.layoutFrame.width / 7
        let height = view.safeAreaLayoutGuide.layoutFrame.height / 5
    
        return CGSize(width: widht, height: height)
    }

 

✅ 그런데 다른 UI와 겹쳐보이면 안되니까 내 컬렉션 뷰의 높이만 이용하려먼?

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let widht = view.safeAreaLayoutGuide.layoutFrame.width / 7
        let height = collectionView.frame.height / 5
//        print(collectionView.frame.height)
        
        return CGSize(width: widht, height: height)
    }