【问题标题】:iOS: How can I change the orientation of a UICollectionViewCell?iOS:如何更改 UICollectionViewCell 的方向?
【发布时间】:2012-10-09 21:12:28
【问题描述】:

我有一个UICollectionViewController,当用户转动他们的 iPad 时我会加载它,以便将其置于横向。我遇到的问题是我的UICollectionViewCells 仍在加载,就好像它是纵向的一样。我在 Interface Builder 中将UICollectionViewController 设置为横向,并且我使用了一个名为CollectionViewCell 的自定义单元格类。每个单元格只包含一个图像和一个标签。

我应该在 Interface Builder 或我的代码中做些什么吗?我尝试使用CGAffineTransformationMakeRotation,但这无济于事。如果其他人以前遇到过这种情况,我将非常感激!非常感谢!

这里有一些代码供参考:

-(void)viewDidLoad
{
 self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
    return listArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{      
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"devices" forIndexPath:indexPath];
   cell.statusImage.image=[UIImage imageNamed:@"Default.png"];
    cell.name.text=@"HEY !!!!!";
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(320, 420);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(50, 20, 50, 20);

}

【问题讨论】:

    标签: ios orientation ios6 uicollectionview uicollectionviewcell


    【解决方案1】:

    如果其他人遇到过这个问题,这就是我的解决方法。

    我最初不允许自动旋转,而只是使用NSNotificationCenter 注册orientationChanged 通知。这很好用,只是它阻止了我的第二个视图意识到它需要在横向模式下加载。

    因此,我为嵌入我的主视图的tabBar 创建了一个类,并为该类中的shouldAutoRotate 方法以及除我想以横向模式加载的一个。我仍在主视图的控制器中使用orientationChanged 通知,因为它嵌入在tabBar 中。但我只是在返回主视图时使用自动旋转。

    如果有人有任何问题,请告诉我。希望对您有所帮助!

    【讨论】:

    • 我在每个单元格中使用具有动态高度的自定义 UICollectionView。但是当我旋转时,我的单元格不会通过 columnWidthForCollectionView 方法更改宽度,也不会通过 maximumNumberOfColumnsForCollectionView 方法更改列数。我正在动态计算我的列宽。通过 -(CGFloat)getCollumnWidth { CGFloat 宽度 = (self.view.frame.size.width-(kCollectionCellBorderRight*3))/2;返回宽度; } 这在 iPhone 和 iPad 中都会发生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多