2014-05-16 4 views
0

에서 작업중인 경우 & 머리글 상자가 UICollectionView에 들어가는 위치를 어떻게 지정합니까?iOS UICollectionView 머리글 및 바닥 글 위치

저는 UICollectionViewFlowLayout입니다. 내가 덮어 썼다

-(void)prepareLayout 

-(NSArray*) layoutAttributesForElementsInRect:(CGRect)rect 

-(UICollectionViewLayoutAttributes*) layoutAttributesForSupplementaryViewOfKind: (NSString*)kind atIndexPath:(NSIndexPath*)indexPath 

내 문제는, 어떻게 헤더 위치를 지정 해야할지 모르겠다. 이미 헤더가 prepareLayout에 있는지 지정한 :

-(void)prepareLayout 
{ 
[super prepareLayout]; 

boundsSize = self.collectionView.bounds.size; 
midX = boundsSize.width/2.0f; 
curIndex = 0; 

self.headerReferenceSize = CGSizeMake(CELL_SIZE, TITLE_HEIGHT); 
self.footerReferenceSize = CGSizeMake(0, 0); 

self.scrollDirection = UICollectionViewScrollDirectionHorizontal; 
self.sectionInset = UIEdgeInsetsMake(TOP_INSET, LEFT_INSET, BOTTOM_INSET, RIGHT_INSET); 
self.minimumLineSpacing = LINE_SPACING; 
self.minimumInteritemSpacing = INTERIM_SPACING; 
self.itemSize = CGSizeMake(CELL_SIZE, CELL_SIZE); 

} 
설정 "HeaderLocation"처럼 뭔가있을 것 같지 않기 때문에 난 그냥, FlowLayout 설정하는 내 사용자 지정의 오른쪽 속성을 모르는

, LayoutAttributes 또는 레이아웃 객체 자체로 나타납니다. 바로 지금, 그것은 내 이미지들 사이에/그들이 각 이미지 (수평 스크롤) 위에 나타나기를 바랄 때 나타나는 것입니다.

-(UICollectionReusableView*) collectionView: (UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath 
{ 
    NSLog(@"**ViewForSupplementaryElementOfKind called***"); 

    CGFloat centerX = collectionView.center.x; 
    CGFloat centerY = collectionView.center.y; 
    CGFloat titleWidth = [MyLayout titleWidth]; 
    CGFloat titleHeight = [MyLayout titleHeight]; 

    MyTitleView* titleView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:ImageTitleIdentifier forIndexPath:indexPath]; 

    titleView.frame = CGRectMake(centerX - titleWidth/2.0, 
           0.0, 
           titleWidth, 
           titleHeight); 

    return titleView; 
} 

이 작동하지 않습니다

나는 다음과 같은 노력했다. 제목이 다른 제목들과 겹쳐진 위에 나타납니다. 그런 다음 스크롤을 시작한 후 (가로로), 위의 이미지가 아닌 이미지 사이에 잘못된 위치로 다시 건너 뜁니다.

PS> NIB 또는 XIB 게재 위치와 관련된 사항을 제안하지 마십시오. UICollectionView를 사용하고 UICollectionViewController가 아니므로 실제로 프로토 타입 셀을 사용할 필요가 없습니다. 레이아웃은 코드만으로 프로그래밍 방식으로 진행되고 있으므로 XIB 파일을 열고 텍스트 상자의 위치를 ​​조정할 수는 없습니다.

답변

-2

CollectionView 헤더 높이 Collectionview 위임 이하로 설정되어

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

그리고 위임 아래

- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionReusableView * view = nil; 

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) 
    { 
     ColorSectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                      withReuseIdentifier:NSStringFromClass([ColorSectionHeaderView class]) 
                        forIndexPath:indexPath]; 
     header.sectionIndex = indexPath.section; 
     header.hideDelete = collectionView.numberOfSections == 1; // hide when only one section 
     header.delegate = self; 
     view = header; 
    } 

return view; 

에서 Collectionview 헤더의 Set 뷰}

Ragistred 클래스에있는 viewDidLoad

-(void)ViewDidLoad 
{ 

[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ColorSectionFooterView class]) bundle:nil] 
    forSupplementaryViewOfKind:UICollectionElementKindSectionFooter 
      withReuseIdentifier:NSStringFromClass([ColorSectionFooterView class])]; 

    [Super ViewDidLoad]; 
} 
+0

감사합니다,하지만 난 이미 높이를 변경하는 방법을 알고, 내가 어디에 위치를 변경하는 방법을 알고 싶습니다. 에서와 같이 y 좌표 = 0에서 내 헤더보기를 내용 위에 배치하도록 프로그램에 지시하고 싶습니다. 솔루션에서 명확하게 특정 위치에 배치 할 헤더를 말하고 있습니까? dequeueReusableSupplementaryView는 저를 제어 할 수 없습니다. – Cindeselia

+0

"MyTitleView"란 무엇입니까? 수업입니까? –

+0

예, 내 머리글 클래스입니다. – Cindeselia

0

-layoutAttributesForElementsInRect이 반환하는 속성을 수정하는 것이 올바른 방법이지만 오프 스크린 머리글과 바닥 글의 위치를 ​​변경하려면 보완 뷰 속성을 직접 가져와야 할 수 있습니다.

당신의 UICollectionViewFlowLayout 서브 클래스의 예를 들어

:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 
{ 
    NSMutableArray *attributesArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy]; 

    // the call to super only returns attributes for headers that are in the bounds, 
    // so locate attributes for out of bounds headers and include them in the array 
    NSMutableIndexSet *omittedSections = [NSMutableIndexSet indexSet]; 
    for (UICollectionViewLayoutAttributes *attributes in attributesArray) { 
     if (attributes.representedElementCategory == UICollectionElementCategoryCell) { 
      [omittedSections addIndex:attributes.indexPath.section]; 
     } 
    } 
    for (UICollectionViewLayoutAttributes *attributes in attributesArray) { 
     if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) { 
      [omittedSections removeIndex:attributes.indexPath.section]; 
     } 
    } 
    [omittedSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { 
     NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx]; 
     UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
                          atIndexPath:indexPath]; 
     [attributesArray addObject:attributes]; 
    }]; 

    for (UICollectionViewLayoutAttributes *attributes in attributesArray) { 
     if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) { 

      // adjust any aspect of each header's attributes here, including frame or zIndex 

     } 
    } 

    return attributesArray; 
} 
+0

많은 코드를 게시 해 주셔서 감사합니다.하지만 편집중인 속성을 지정하십시오. 그것이 가장 중요한 부분입니다.당신은 // '프레임 또는 zIndex'를 포함하여 각 헤더의 속성을 조정하지만 헤더가있는 곳의 프레임을 편집합니까? 이미 존재하는 내용이 겹쳐 지나요? 특정 속성은 무엇입니까? ('headerLocation'또는 'headerOrigin'또는 'headerFrame'또는 그와 비슷한 이름의 설명서에 하나도 표시되지 않습니다. – Cindeselia

+0

"... 프레임을 편집하면 헤더가있는 위치가 변경됩니까? 이미 존재하는 내용이 겹쳐 지기만합니까? " 그러나 UICollectionViewLayoutAttribute의 프레임 및 zIndex 속성이 수행하는 작업과 정확히 같습니다. 먼저 [the docs] (https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewLayoutAttributes_class/Reference/Reference.html)를 읽으라고 제안 할 수 있습니까? – followben

+0

[이 기사] (http://www.objc.io/issue-3/collection-view-layouts.html)는 UICollectionViewLayoutAttributes에 대한 자세한 이해와 그 작동 방식에 도움이 될 수 있습니다. – followben