2013-01-17 14 views
26

मैं UICollectionView गतिशील रूप से सेक्शन हेडर की ऊंचाई सेट करने का प्रयास कर रहा हूं, लेकिन नीचे दिए गए कोड का उपयोग करके, मुझे कुछ भी बदलाव दिखाई नहीं दे रहा है। दृश्य में सही वस्तुओं के साथ खींचा गया है लेकिन ऊंचाई खराब नहीं होगी। क्षमा करें अगर यह दोहराया गया प्रश्न है, लेकिन मुझे विशेष रूप से UICollectionView ऑब्जेक्ट से संबंधित कुछ भी नहीं मिल रहा है। अग्रिम में धन्यवाद। इस तरहएक UICollectionReuseableView (संग्रह अनुभाग शीर्षलेख) की गति को गतिशील रूप से

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView 
      viewForSupplementaryElementOfKind:(NSString *)kind 
           atIndexPath:(NSIndexPath *)indexPath 
{ 
    PhotoVideoHeaderCell *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                      withReuseIdentifier:@"videoHeaderView" 
                       forIndexPath:indexPath]; 
    if (indexPath.section == 0) { 
     // photos 
     [headerView setSection:@"Photo"]; 
    } else { 
     [headerView.VehicleDetailView removeFromSuperview]; 
     CGRect frame = headerView.frame; 
     frame.size.height = 60; 
     [headerView setFrame:frame]; 
     [headerView setNeedsDisplay]; 
     [headerView setBackgroundColor:[UIColor grayColor]]; 
     [headerView setSection:@"Video"]; 
    } 

    return headerView; 
} 

उत्तर

64

आपका प्रतिनिधि, निम्नलिखित समारोह को लागू करना चाहिए आप एक प्रवाह लेआउट का उपयोग कर रहे संभालने:

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

आप के लिए एक अलग आकार लौट सकते हैं प्रत्येक हेडर क्षैतिज रूप से स्क्रॉलिंग संग्रह दृश्यों में, केवल चौड़ाई का उपयोग किया जाता है। लंबवत स्क्रॉलिंग वाले में, केवल ऊंचाई का उपयोग किया जाता है। अप्रयुक्त मूल्य को अनदेखा किया जाता है - क्रमशः क्षैतिज/ऊर्ध्वाधर संग्रह दृश्यों की पूर्ण ऊंचाई/चौड़ाई को भरने के लिए आपका दृश्य हमेशा बढ़ाया जाएगा।

फ़ुटर्स के लिए भी एक समान विधि है।

+1

इस काम करता है, लेकिन मैं कैसे बता जो अनुभाग मैं संपादन कर रहा हूँ, मैं केवल सूचकांक 1 – DirtyKalb

+0

उपयोग 'section' पैरामीटर पर अनुभाग के लिए यह क्या करना चाहते हैं विधि में स्टोर करें जिसे आप एक ivar में संपादित कर रहे हैं और फिर उनकी तुलना करें। यदि वे समान नहीं हैं, तो लेआउट के 'हेडर रिफरेंस आकार' को वापस करें। –

+0

यहां एक पूर्ण नोब होने के लिए खेद है लेकिन हेडर रिफरेंस साइज संग्रह दृश्य का हिस्सा नहीं है ... क्या मुझे फ्रेम पढ़ने के लिए हेडर का एक उदाहरण बनाने की आवश्यकता है? – DirtyKalb

1

कोशिश कुछ:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { 
    UICollectionReusableView *header = [siteMapCollection dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerID" forIndexPath: indexPath]; 
    NSString *headerText = @"This is my header"; 
    UIFont *labFont = [UIFont fontWithName: @"HelveticaNeue-CondensedBold" size: 20.0]; 
    CGSize textSize = [dummyText sizeWithFont: labFont]; 
    UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, header.frame.size.height - (textSize.height + 12), header.frame.size.width, textSize.height + 8)]; 
    [headerLabel setFont: labFont]; 

    [header addSubview: headerLabel]; 

    return header; 
} 
1
स्विफ्ट 3 में

स्वीकृत जवाब और 4:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSize(width: collectionView.frame.size.width, height: 250) 
} 
संबंधित मुद्दे