iOS

[트러블슈팅] UICollectionViewCell 그림자가 초기 렌더링 시 안 보이는 문제

양밀루 2025. 7. 18. 20:55

🚨문제상황

  • 셀 내부이미지에 shadow를 적용했지만 최초 실행 시 보이지 않음
  • 스크롤 후 다시 돌아오면 그림자가 정상적으로 보임
  • 아래 사진 순서 대로 1,2번은 안보이고 스크롤 하면 3번부터 보임, 다시 돌아오면 2,1 순으로 그림자가 생김

 

 

🔎 문제 원인

  • AutoLayout이 완전히 적용되기 전에 shadowPath 설정
  • containerView.bounds가 아직 계산되지 않은 상태여서 (0,0,0,0)이 찍힘
override func layoutSubviews() {
    super.layoutSubviews()
    containerView.layer.shadowPath = UIBezierPath(
        roundedRect: containerView.bounds,  // ❌ 초기에 bounds가 (0,0,0,0)
        cornerRadius: 16
    ).cgPath
}

 

 

 

🧚🏻 해결 방법

override func layoutSubviews() {
    super.layoutSubviews()
    
    // containerView.bounds 대신 Constraints와 같은 계산된 크기 직접 사용
    containerView.layer.shadowPath = UIBezierPath(
        roundedRect: CGRect(
            x: 0, y: 0,
            width: contentView.bounds.width,     
            height: contentView.bounds.width * 1.15 
        ),
        cornerRadius: 16
    ).cgPath
}

DispatchQueue를 사용하는 방법도 있지만 간단하게 해결하고 싶어서 택한 방법이다!