iOS:我如何改变UICollectionViewCell的方向?

5
我有一个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);

}
1个回答

4
如果其他人也遇到了这个问题,这是我解决问题的方法。
起初我未允许自动旋转,而是只使用NSNotificationCenter注册orientationChanged通知。这个方法非常好用,但阻止了我的第二个视图意识到需要在横向模式下加载。
所以,我创建了一个类来处理嵌入主视图的tabBar,并在该类中以及每个其他视图中返回NO以使shouldAutoRotate方法无效,除了那个需要在横向模式下加载的视图之外。我的主视图控制器仍在使用orientationChanged通知,因为它被嵌入在tabBar中。但是当返回到主视图时,我会自动旋转。
如果有任何问题,请告诉我。希望能帮到你!

我正在使用自定义的UICollectionView,每个单元格都具有动态高度。但是当我旋转时,我的单元格不会通过columnWidthForCollectionView方法和maximumNumberOfColumnsForCollectionView方法更改宽度和列数。我正在动态计算我的列宽,通过-(CGFloat)getCollumnWidth { CGFloat width = (self.view.frame.size.width-(kCollectionCellBorderRight*3))/2; return width; } 这在iPhone和iPad上都发生了。 - Zeeshan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接