I have an app, where the main screen should contain a logo and six button layed out in a grid of 2x3 in portrait and 3x2 in landscape mode.
Using auto layout, I can place the logo (uiumage) correctly and the uicollectionview correctly, and they lay out correctly, and this works nicely even when the device changes rotation.
This is what I got (using the RDHCollectionViewGridLayout class):
-(void)viewDidLoadImpl{
self.navCollectionView.delegate = self;
self.navCollectionView.dataSource = self;
titlesArray = [NSArray arrayWithObjects:
@"Meine Hunde",
@"Füttern",
@"Einstellungen",
@"Fragen & Antworten",
@"Über diese App",
@"Rechtliches", nil];
seguesArray = [NSArray arrayWithObjects:
@"dogs",
@"feed",
@"settings",
@"faq",
@"about",
@"law", nil];
imagesArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"hunde_button"],
[UIImage imageNamed:@"fuettern_button"],
[UIImage imageNamed:@"einstellungen_button"],
[UIImage imageNamed:@"fragen_button"],
[UIImage imageNamed:@"ueber_button"],
[UIImage imageNamed:@"rechtlich_button"], nil];
imagesHoverArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"hunde_button_hover"],
[UIImage imageNamed:@"fuettern_button_hover"],
[UIImage imageNamed:@"einstellungen_button_hover"],
[UIImage imageNamed:@"fragen_button_hover"],
[UIImage imageNamed:@"ueber_button_hover"],
[UIImage imageNamed:@"rechtlich_button"], nil];
RDHCollectionViewGridLayout *layout = [RDHCollectionViewGridLayout new];
layout.lineItemCount = 2;
layout.lineDimension = 0;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionsStartOnNewLine = YES;
[self.navCollectionView setCollectionViewLayout:layout];
}
#pragma mark - UICollection View Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:10];
label.text = [titlesArray objectAtIndex:indexPath.row];
UIButton *button = (UIButton *)[cell viewWithTag:20];
// do something with the button
return cell;
}
#pragma mark - UICollectionViewDelegatFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft) {
// orientation is landscape left
CGSize s = {100,200};
return s;
} else if(orientation == UIInterfaceOrientationLandscapeRight) {
// orientation is landscape right
CGSize s = {100,200};
return s;
} else if(orientation == UIInterfaceOrientationMaskPortrait) {
// orientation is portrait
CGSize s = {100,100};
return s;
} else if(orientation == UIInterfaceOrientationMaskPortraitUpsideDown) {
// orientation is portrait upsidedown
CGSize s = {100,100};
return s;
} else {
CGSize s = {100,100};
return s;
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortrait) {
RDHCollectionViewGridLayout *layout = [RDHCollectionViewGridLayout new];
layout.lineItemCount = 2;
layout.lineDimension = 0;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionsStartOnNewLine = YES;
[self.navCollectionView setCollectionViewLayout:layout];
} else {
RDHCollectionViewGridLayout *layout = [RDHCollectionViewGridLayout new];
layout.lineItemCount = 3;
layout.lineDimension = 0;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.sectionsStartOnNewLine = YES;
[self.navCollectionView setCollectionViewLayout:layout];
}
}
However, rotation changes are not layed out correctly, see screenshots:
After loading:
After switching to landscape:
After switching back to portrait:
I am even not sure, if using a collectionview is the right approach? I would prefer to use something like a static tableview, but this gives me only 1 column?