2016-08-29 7 views
4

私は予定リストの種類のアプリの詳細画面を作成しようとしています。これは、ヘッダと、UICollectionViewControllerある自動サイズ調整のUICollectionViewヘッダー

detail screen

:ここでは、詳細画面は、現在のようになります。ヘッダには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. ビューをコレクションビューのスーパービューに追加します。
  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

セルフサイジングセルは主に非常に基本的なケースを満たしているようですが、ヘッダーやフッターにこの種のハックを使用する必要があります。 –

関連する問題