Please help me how I can add labels horizontally and similarly buttons horizontally but each button should align at down of each label like a another section. This should happen dynamically in the header of the UICollectionView as the number of labels and buttons is according to my data.
I want to make a excel kind of layout and in header I want to show the above controls.
Thanks in advance!
I have done this-
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
DepartmentCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
for (int i=0; i<_officelist.count; i++) {
UILabel *label = [[UILabel alloc] init];
label.tag = i+1;
label.text=[NSString stringWithFormat:@"%@",_officelist[i]];
[self.roadmapCollectionView addSubview:label];
}
reusableview = headerView;
}
return reusableview;
}
OfficeList is my array containing the list which I want to display in each label at index value.
UICollectionView
provides callbacks-Whenever it's going to display a
supplementaryView
Whenever a
supplementaryView
scrolls out of the screen-You need to look out for
elementKind
parameter here. It has two valuesUICollectionElementKindSectionHeader
&UICollectionElementKindSectionFooter
.I believe you need to do your customisations with
UICollectionElementKindSectionHeader
.Hope it helps.
UPDATE:
If
UICollectionView
doesn't provide a way to customise this on the fly as described above, the recommended approach would be to design theUICollectionElementKindSectionHeader
in the storyboard itself.All you need to do is drag a
UICollectionReusableView
inside yourcollectionView
in the view hierarchy. You might also need to consider setting a custom subclass for this and addIBOutlet
connections to different UI components.This is working perfectly fine :-
Try this code.
UPDATE: Try to set constraints for uilabel programmatically.
I would recommend you to create a uiview in storyboard and inflate it everytime when measurements shows that it has to be inflated.