2015-08-01 16 views
7

Tôi đang mã hóa cho iOS 8+.UICollectionView Chiều cao tiêu đề AutoSize

Tôi có một UICollectionReusableView đó được sử dụng như là tiêu đề của UICollectionView

class UserHeader: UICollectionReusableView { 
... 
} 

Xem Bộ sưu tập của tôi làm một vài điều:

tải NIB trong viewDidLoad

override func viewDidLoad() { 
super.viewDidLoad() 
resultsCollectionView.registerNib(UINib(nibName: "UserHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "UserHeader") 
} 

Sets tiêu đề chiều cao trong referenceSizeForHeaderInSection .

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSizeMake(0, 500) 
} 

Tuy nhiên, UserHeader quan điểm của tôi bao gồm nhiều UILabel, UIViews ai là chiều cao thay đổi tại thời gian chạy, làm thế nào tôi có thể chỉ định một chiều cao cho referenceSizeForHeaderInSection đó là năng động? Hoặc nếu tôi không được phép sử dụng referenceSizeForHeaderInSection để tự động định kích thước trong iOS 8+, vui lòng cho tôi biết những gì tôi nên sử dụng. Cảm ơn trước.

Đối với đầy đủ sake, dưới đây là cách tôi tải các quan điểm, nhưng tôi không chắc chắn nếu có liên quan để thảo luận này:

func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! { 
    var reusableview:UICollectionReusableView = UICollectionReusableView() 
    if (kind == UICollectionElementKindSectionHeader) { 
     let userHeaderView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "UserHeader", forIndexPath: indexPath) as! UserHeader 

        ... extra code to modify UserHeader 

        reusableview = userHeaderView 
    } 
    return reusableview 
} 

Trả lời

0

tôi phải đối mặt với một vấn đề tương tự và cố định nó bằng cách sử dụng NSLayoutConstraint s trong tầm nhìn để chỉ định chiều cao của chế độ xem tiêu đề. Trong trình điều khiển chế độ xem, tôi sau đó định cấu hình tiêu đề của chế độ xem bộ sưu tập như vậy:

fileprivate var expectedSectionHeaderFrame = CGRect.zero 
let headerIdentifier = "HeaderView" 

func calculateHeaderFrame() -> CGRect { 
    headerView.setNeedsLayout() 
    headerView.layoutIfNeeded() 

    let expectedHeaderSize = CGSize(width: view.bounds.width, height: headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height) 

    return CGRect(origin: .zero, size: expectedHeaderSize) 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    expectedSectionHeaderFrame = calculateHeaderFrame() 
    return expectedSectionHeaderFrame.size 
} 

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    guard kind == UICollectionElementKindSectionHeader else { fatalError("Unexpected kind of supplementary view in (type(of: self))") } 

    let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) 
    headerView.frame = expectedSectionHeaderFrame 

    return headerView 
} 
Các vấn đề liên quan