如何使用网格中的按钮创建视图控制器?

时间:2015-01-07 18:26:32

标签: ios uicollectionview uicollectionviewlayout

我有一个应用程序,其中主屏幕应包含一个徽标和六个按钮,网格为2x3纵向和3x2横向模式。

使用自动布局,我可以正确放置徽标(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];
}
}

但是,旋转更改没有正确布局,请参见屏幕截图:

加载后:After loading the VC

切换到横向后:After switching to landscape

切换回肖像后:enter image description here

我甚至不确定,如果使用collectionview是正确的方法?我更喜欢使用类似静态tableview的东西,但这只给我一列?

2 个答案:

答案 0 :(得分:0)

您需要一个集合视图(即二维表)。首次加载网格时,每个单元格的大小是100 x 100?这是你在界面构建器中指出的吗?它最初可以适当调整大小,在转向横向时调整,然后在转回肖像时调整不正确。

答案 1 :(得分:-1)

我的解决方案是不使用RDHCollectionViewGridLayout并使用UICollectionViewDelegateFlowLayout类及其方法来计算值以使它们适合。