Compatible UICollectionView Header Views

I built a UICollectionView in a storyboard and implemented all the necessary data sources and delegation methods in the view controller. In the storyboard, I checked the Section Header property in the collection view and set the title view class to a subclass of UICollectionResusableView (in the storyboard).

From here, I dragged two user interface elements into the header view through the storyboard - the label and the segmented control:

enter image description here

When the program runs, the label appears in the header view of the collection view (without the actual code), but the segmented control does not work. However, when a segmented control is dragged onto a typical UIView , it is displayed and controlled without the need for code. Even when you create the code instance in IBOutlet segmented control does not appear.

Why does the segmented control not appear in the header of the collection view when it is in a typical UIView , and why does displaying labels work without problems?

UPDATE

Here is the init method for the custom header in which I tried to add a segmented control programmatically (as opposed to the storyboard):

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]]; [_segmentedControl setFrame:CGRectMake(0, 0, 100, 50)]; [_segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:UIControlEventValueChanged]; [self addSubview:_segmentedControl]; } return self; } 

According to the request, there is a method -[UICollectionReusableView viewForSupplementaryElementOfKind:] in the main view controller:

 - (UICollectionReusableView *)collectionView:(UICollectionView *)cv viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { GalleryHeader *headerView = [cv dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; return headerView; } 
+4
source share
1 answer

I cannot reproduce the problem with the storyboard, it works great for me when I add a segmented control by dragging it directly into the storyboard (without the need for code). As for your alternative way to add it programmatically, the problem is that when the view is initialized from the storyboard (as in this case), the initWithCoder initialization initWithCoder (not the initWithFrame initialization initWithFrame ). Therefore, if you override this method by inserting code there, it should work:

 -(id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if(self){ _segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]]; _segmentedControl.bounds = CGRectMake(0, 0, 100, 50); [_segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:UIControlEventValueChanged]; [self addSubview:_segmentedControl]; } return self; } 

PS This does not affect this particular case, but you should just do:

 GalleryHeader *headerView = [cv dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; 

instead:

 GalleryHeader *headerView = [cv dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; 

Since this is a collection view that will request the correct view, you should worry about defining it!

EDIT: The steps that I followed to create the title from the storyboard are as follows:

  • Select a collection view and check the "Title box" box.

  • Select the newly created title and select the desired class in the Identity Inspector

enter image description here

  • Give the header sector a unique identifier

enter image description here

  • Drag and drop the user interface elements in the header in the storyboard (I also changed its background color)

enter image description here

  • Finally, we implement the collectionView:viewForSupplementaryElementOfKind:atIndexPath: in your collection data view data class
  -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { return [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionViewHeader" forIndexPath:indexPath]; } 

Let me know if you can make any difference between what you did and what I did!

+4
source

Source: https://habr.com/ru/post/1486161/


All Articles