2016-08-29 3 views
4

할 일 목록 종류의 앱에 대한 세부 화면을 만들려고합니다.자동 크기 조정 UICollectionView 헤더

detail screen

이는 UICollectionViewController 인 헤더 : 다음과 같은 세부 화면이 현재 모습입니다. 헤더는 2 UILabel 개체와 UITextView 개체를 포함합니다. 이러한 개체의 레이아웃은 세로 UIStackView에 의해 관리됩니다. UIView은 흰색 배경을 설정하는 데 사용됩니다.

런타임시이 UICollectionReusableView 높이를 정의하는 데 어려움이 있습니다. 모든 조언을 부탁드립니다.

+0

http://stackoverflow.com/a/33696385/6064629 아마도 도움이 될 것입니다. – Himanshu

+0

고마워요 @himanshu. 나는 그 해결책이 기본적으로'UICollectionReusableView'의 여분의 복사본을 인스턴스화하고 바로 뒤따라 잇기 때문에 더 나은 대답이되기를 바라고 있습니다. –

+0

@KelvinLau 당신은 무엇인가 알아낼 수 있었습니까? XIB 파일이 필요하기 때문에 제공된 솔루션에 대한 열렬한 팬이 아닙니다. – Michael

답변

0

다음은 XIB 파일없이 자동 레이아웃으로 맞춤 UICollectionViewReusableView를 처리하는 방법입니다.

  1. referenceSizeForHeaderInSection 대리자 메서드를 구현합니다.
  2. 헤더보기로 사용하는보기를 인스턴스화하십시오.
  3. 깜박임을 방지하기 위해 표시 여부를 숨김으로 설정합니다.
  4. 보기를 collectionview의 수퍼 뷰에 추가하십시오.
  5. 자동 레이아웃을 사용하여 레이아웃을 설정하여 예상되는 시각적 결과를 일치시킵니다.
  6. 호출 setNeedsLayoutlayoutIfNeeded
  7. 수퍼에서

주의보기를 제거 : 나는,이 솔루션의 큰 팬이 아니다는 collectionview의 수퍼 각 시간을 사용자 정의보기를 추가로 계산을 수행 . 그래도 성능 문제는 알지 못했습니다.

주의 # 2 : 임시 해결 방법으로 취급하고 발행 된 자체 보완보기로 마이그레이션합니다.

자동 레이아웃 용으로 PureLayout을 사용하고 있습니다.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 

    let header = CustomHeaderView() 

     header.isHidden = true; 
     self.view.addSubview(header) 
     header.autoPinEdge(toSuperviewEdge: .leading) 
     header.autoPinEdge(toSuperviewEdge: .trailing) 
     header.autoPin(toTopLayoutGuideOf: self, withInset: 0) 
     header.setupHeader(withData: self.data) 
     header.setNeedsLayout() 
     header.layoutIfNeeded() 
     header.removeFromSuperview() 

     return header.frame.size 
} 
1

이것은 약간의 해킹이지만 작동하는 것 같습니다.

// showhere to keep a reference 
UICollectionReusableView * _cachedHeaderView; 


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ 

    if(!_cachedHeaderView){ 
     // dequeue the cell from storyboard 
     _cachedHeaderView = [collectionView dequeueReusableCellWithReuseIdentifier:[NSString stringWithFormat:@"header_cell"] forIndexPath:indexPath]; 

     // set captions/images on the header etc... 

     // tell the collectionview to redraw this section 
     [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]]; 
    } 

    return _cachedHeaderView; 
} 


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ 

    // once there is a reference ot the view, use it to figure out the height 
    if(_cachedHeaderView){ 
     return [_cachedHeaderView systemLayoutSizeFittingSize:collectionView.bounds.size withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:UILayoutPriorityDefaultLow]; 

    } 

    // a placeholder value just to get the dequeueReusableCellWithReuseIdentifier to work 
    return CGSizeMake(collectionView.bounds.size.width, 100); 
} 
+0

셀프 사이징 셀은 대부분 매우 기본적인 경우를 충족시키는 것처럼 보이지만 헤더 나 푸터에 이러한 종류의 해킹을 의지해야합니다. –