简体   繁体   English

iOS:如何更改UICollectionViewCell的方向?

[英]iOS: How can I change the orientation of a UICollectionViewCell?

I have a UICollectionViewController that I load when a user turns their iPad so as to put it into landscape orientation. 我有一个UICollectionViewController ,我在用户转动iPad时将其加载到横向。 The problem I'm having is that my UICollectionViewCell s are still loading as if it were in portrait orientation. 我遇到的问题是我的UICollectionViewCell仍然像纵向一样加载。 I set the UICollectionViewController to landscape inside Interface Builder, and I'm using a custom cell class that I've called CollectionViewCell . 我在接口生成器中将UICollectionViewController设置为landscape,我正在使用我称为CollectionViewCell的自定义单元类。 Each cell contains just an image and a label. 每个单元格只包含一个图像和一个标签。

Is there something that I should be doing either in Interface Builder or in my code? 我是否应该在Interface Builder或我的代码中做些什么? I tried using CGAffineTransformationMakeRotation , but that was no help. 我尝试使用CGAffineTransformationMakeRotation ,但这没有用。 If anyone else has encountered this before, I would be extremely grateful! 如果其他人之前遇到过这种情况,我将非常感激! Thanks very much! 非常感谢!

Here's a bit of the code for reference: 这里有一些参考代码:

-(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);

}

In case anyone else has had this problem, here is how I solved it. 如果其他人遇到这个问题,我就是这样解决的。

I was initially not allowing autorotation, and instead was just registering for orientationChanged notifications using NSNotificationCenter . 我最初不允许自动旋转,而只是使用NSNotificationCenter注册orientationChanged通知。 This worked great except that it was preventing my second view from realizing that it needed to load in landscape mode. 这很好用,除了它阻止我的第二个视图意识到它需要以横向模式加载。

So instead of that, I've created a class for the tabBar that my main view is embedded in, and I return NO for the shouldAutoRotate method in that class and in every other view except for the one which I want to load in landscape mode. 所以不是那样,我已经为我的主视图嵌入的tabBar创建了一个类,并且我为该类和所有其他视图中的shouldAutoRotate方法返回NO ,除了我想在横向模式下加载的那个。 I'm still using the orientationChanged notifications in my main view'S controller, since it is embedded in the tabBar . 我仍然在主视图'' tabBar控制器中使用orientationChanged通知,因为它嵌入在tabBar But I'm just using autorotation when returning to the main view. 但是我只是在返回主视图时使用自动旋转。

If anyone has any questions, let me know. 如果有人有任何疑问,请告诉我。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM