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

- Give the header sector a unique identifier

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

- 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!
source share