2017-09-23 5 views
-1

작은 대화 메시지 응용 프로그램을 만들고 있습니다. 그 목적을 위해 나는 콜렉션 뷰를 사용하고있다. 나는 많은 코드를 발견하고 함께 작동시켜 작동되도록했습니다. 그러나 이제는 사용자 정의 셀에 몇 가지 추가 레이블을 시간에 대해 추가하고 그룹 채팅에서 사용자 이름을 추가하려고합니다. 그러나 버블 뷰에 레이블을 추가 할 수없는 것 같습니다. timeLabel은 거품보기 아래에 있지만, 시간표가 textView 아래의 거품보기 내부에 있어야하는 방식으로 제약 조건을 설정합니다.채팅 응용 프로그램의 콜렉션보기에서 셀 크기를 조정하는 데 문제가 있습니다.

override init(frame: CGRect) { 
    super.init(frame: frame) 

    addSubview(bubbleView) 
    addSubview(textView) 
    addSubview(profileImageView) 
    addSubview(timeView) 
    addSubview(usernameView) 



    //x,y,w,h 
    profileImageView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true 
    // profileImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true 
    profileImageView.widthAnchor.constraint(equalToConstant: 32).isActive = true 
    profileImageView.heightAnchor.constraint(equalToConstant: 32).isActive = true 
    profileImageView.topAnchor.constraint(equalTo: bubbleView.topAnchor).isActive = true 

    //x,y,w,h 

    bubbleViewRightAnchor = bubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8) 
    bubbleViewRightAnchor?.isActive = true 
    bubbleViewLeftAnchor = bubbleView.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8) 
    // bubbleViewLeftAnchor?.isActive = false 
    bubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true 
    bubbleWidthAnchor = bubbleView.widthAnchor.constraint(equalToConstant: 200) 
    bubbleWidthAnchor?.isActive = true 
    bubbleView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true 

    //ios 9 constraints 
    //x,y,w,h 
    // textView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true 
    textView.leftAnchor.constraint(equalTo: bubbleView.leftAnchor, constant: 8).isActive = true 
    textView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true 
    textView.rightAnchor.constraint(equalTo: bubbleView.rightAnchor).isActive = true 
    // textView.widthAnchor.constraint(equalToConstant: 200).isActive = true 
    textView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true 
    textView.bottomAnchor.constraint(equalTo: timeView.topAnchor, constant: 8).isActive = true 

    timeView.rightAnchor.constraint(equalTo: textView.rightAnchor, constant: 8).isActive = true 
    //timeView.topAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: 8).isActive = true 
    timeView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true 
    timeWidthAnchor = timeView.widthAnchor.constraint(equalToConstant: 40) 
    timeWidthAnchor?.isActive = true 

    /* usernameView.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true 
    usernameView.bottomAnchor.constraint(equalTo: textView.topAnchor).isActive = true 
    nameWidthAnchor = usernameView.widthAnchor.constraint(equalToConstant: 100) 
    nameWidthAnchor?.isActive = true 
    usernameView.heightAnchor.constraint(equalToConstant: 20) 
    nameHeightAnchor?.isActive = true */ 
} 

나는 문제가 컬렉션 뷰에서 sizeForItemAt 방법이라고 생각 : 여기

는 코드입니다. 그러나 솔직히 말해서 나는 그 방법을 바꾸는 방법을 모른다. 그래도 현재로서는 텍스트 뷰와 함께 작동한다.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    var height: CGFloat = 80 
    let text: String? 
    //get estimated height somehow???? 
    if messages[indexPath.item].text != nil { 
     text = messages[indexPath.item].text 
     height = estimateFrameForText(text!).height + 20 
    } 

    let width = UIScreen.main.bounds.width 
    return CGSize(width: width, height: height) 
} 

fileprivate func estimateFrameForText(_ text: String) -> CGRect { 
    let size = CGSize(width: 200, height: 1000) 
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin) 
    return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)], context: nil) 
} 

sample chat

+0

당신이 함께 붙여 넣은 코드를 이해 대략적인 크기로 estimatedItemSize을 설정합니다. –

+0

이것은 더 나은 이해를위한 시도입니다. 제가 말했듯이 그것은 단지 예상 높이 함수 일뿐입니다. 그것은 나에게 완전히 명확하지 않습니다. 나는 당신이 그것을해야만하는 이유를 이해하고 그 결과로 나는 그것이 무엇을하는지 압니다. 하지만이 함수의 출력을 변경하려고 할 때 실패했습니다. – AlexVilla147

답변

0

다음을 시도해보십시오 :

  1. UICollectionViewCell 서브 클래스에서, 확인, 세포에서 세포를 직접
  2. contentView에 하위 뷰를 추가하지 그의 제약 하위보기는 contentView
  3. 으로 UICollectionViewFlowLayout을 사용하는 경우을 설정합니다.
  4. 당신이 UICollectionViewFlowLayout를 사용하는 경우후
+0

고마워, 해봤지만이 오류가 표시되었습니다 : 'UICollectionViewLayoutAttributes : -setFrame : CGRectNull이 정의되지 않았습니다.' – AlexVilla147

+0

포인트 3과 포인트 4의 차이점은 무엇입니까? 방금 포인트 3을 사용하여 sizeForitemAt 오버라이드하고 itemSize를 반환했습니다. – AlexVilla147

+0

마침내 모든 점을 이해했지만 이드는 문제를 해결하지 못했습니다. timeView는 여전히 버블 뷰 아래에 있으며, 실제로 나를 좌절시킬 수 있습니다. 그리고 갑자기 한 줄에 여러 개의 세포가 서로 절단됩니다. – AlexVilla147