我有一個應用程序,其中主屏幕應包含一個徽標和六個按鈕放置在肖像2×3和橫向模式3×2的網格。如何用網格中的按鈕創建視圖控制器?
使用自動佈局,我可以正確地放置徽標(uiumage)和uicollectionview,並且它們佈置正確,即使在設備更改旋轉時也可以很好地工作。
這是我得到(使用RDHCollectionViewGridLayout類):
-(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];
}
}
然而,旋轉的變化並不奠定了正確,見截圖:
加載後:
切換後景觀:
切換回肖像後:
我甚至不確定,如果使用collectionview是正確的方法?我寧願使用類似靜態tableview的東西,但這隻給我1列?
如果您願意,可以使用表格視圖。您可以在自定義表格視圖單元格中添加兩個或三個按鈕,使其看起來像網格。如果我這樣做,我可能會製作兩個單元格原型,一個帶有兩個按鈕,另一個帶有三個按鈕,然後根據方向將所需的一個出列(我將使用動態表格,而不是靜態表格)。 – rdelmar