UITableViewCell 에서 layoutIfNeeded() 호출 시 layout warning 문구가 나오는 현상

1 답변 글타래를 보이고 있습니다
  • 글쓴이
    • 인담
      참가자
      • 글작성 : 9
      • 답글작성 : 9

      안녕하세요.

      오늘 질문하고 싶은 것은 개인 프로젝트가 발생한 이슈인데 해결방법을 도저히 찾을 수 없어서 질문을 올립니다.

      제가 구현하려고 한 것은  tableViewCell 안에 collection view를 스크롤 되지 않고 해당 컨텐츠 크기만큼 collection view의 높이를 dynamic하게 구현하려고 했습니다.(ex: tag)

      그래서 custom cell안에 해당 collectionView 높이를 변경하기 위해서 layoutIfNeeded()를 호출했는데 호출 순간 

      [LayoutConstraints] Unable to simultaneously satisfy constraints.

      Probably at least one of the constraints in the following list is one you don’t want.

      Try this:

      (1) look at each constraint and try to figure out which you don’t expect;

      (2) find the code that added the unwanted constraint or constraints and fix it.

      (

          “<SnapKit.LayoutConstraint:0x282ce6b80@SummaryTableViewCell.swift#95 UIView:0x155d50090.top == UITableViewCellContentView:0x155d53250.top + 10.0>”,

      의 무수한 경고들이 나타났습니다..ㅠ

      혹시나 collection View가 문제인가 싶어 collectionView를 빼고 그냥 label하나만 넣고 layoutIfNeeded()를 호출해도 해당 경고가 나타납니다.

      제 생각에는 테이블 뷰 셀이 가변적이여야 하는데 layoutIfNeeded() 호출 순간 아래에 나타나는 셀의 높이랑 충돌해서 나타나는 것 같은데 정확한 해결방법을 모르겠네요.

      “<NSLayoutConstraint:0x282bfab20 ‘UIView-Encapsulated-Layout-Height’ UITableViewCellContentView:0x155d53250.height == 44   (active)>”

      현재 문제사항들을 정리하자면

      1. custom cell 안의 ui component에 layoutIfNeeded()를 호출하면 layout warning이 나타난다(tableviewcell height관련)
      2. 무수한 warning이 나타나지만 원하는 결과는 나타난다..
      3. dynamic height를 위해 UITableView.automaticDimension, cellForRowAt에서 cell.layoutIfNeeded() 호출등 방법을 사용했지만 해당 warning은 그대로…
      4. 코드 첨부

          private func setupCollectionViewLayout(){         let layout = UICollectionViewFlowLayout()         let aCollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)         aCollectionView.backgroundColor = .white         aCollectionView.register(PersonCollectionViewCell.self, forCellWithReuseIdentifier: "PersonCollectionViewCell")         aCollectionView.translatesAutoresizingMaskIntoConstraints = false         whoContentColletionView = aCollectionView         whoContentView.addSubview(aCollectionView)         guard let whoContentColletionView = self.whoContentColletionView else{             return         }         whoContentColletionView.snp.makeConstraints{             $0.top.leading.trailing.bottom.equalToSuperview()         }         whoContentColletionView.delegate = self         whoContentColletionView.dataSource = self         whoContentColletionView.reloadData()         whoContentColletionView.layoutIfNeeded()     }

      문제가 생기는 부분은 마지막에
      whoContentColletionView.layoutIfNeeded()
      가 호출되는 순간이고 해당 코드가 실행되는 부분은 override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) 입니다

      감사합니다

      • 이 게시글은 인담에 의해 3 years, 7 months 전에 수정됐습니다.
      • 이 게시글은 인담에 의해 3 years, 7 months 전에 수정됐습니다.
      • 이 게시글은 인담에 의해 3 years, 7 months 전에 수정됐습니다.
      • 이 게시글은 인담에 의해 3 years, 7 months 전에 수정됐습니다.
    • 야곰
      키 마스터
      • 글작성 : 37
      • 답글작성 : 580

      여러줄 코드는 백틱 하나가 아니라 세개로 감싸주면 됩니다. [참고링크]


      whoContentColletionView.snp.makeConstraints{ $0.top.leading.trailing.bottom.equalToSuperview() }

      확실하진 않은데요, 위 코드에서 super view가 아니라, table view cell의 content view와 제약을 만들어보실래요?

      물론 다 뒤져보셨겠지만 참고할 만한 링크들 남겨봅니다.

      또, 오류 메시지나 경고 메시지를 아래 링크에 복사 붙여넣기 해보면 뭐가 문젠지 알 수도 있어요.
      Why The Failure, Auto Layout?

      • 인담
        참가자
        • 글작성 : 9
        • 답글작성 : 9

        감사합니다.

        우선, 지금은 collectionView 의 컨텐츠의 갯수와 너비를 이용해 높이를 구해서
        collectionView.heightAnchor에 constant를 정해서 해당 오류를 해결했습니다.

        첨부하신 링크로 다른 방법이 있는지 참고하겠습니다.
        감사합니다!

1 답변 글타래를 보이고 있습니다
  • 답변은 로그인 후 가능합니다.

logo landscape small

사업자번호 : 743-81-02195
통신판매업 신고번호 : 제 2022-충북청주-1278 호
고객센터 : 카카오톡채널 @yagom