Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The problem I’m having is that my UICollectionViewCells are still loading as if it were in portrait orientation. I set the UICollectionViewController to landscape inside Interface Builder, and I’m using a custom cell class that I’ve called 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? I tried using CGAffineTransformationMakeRotation, but that was no help. 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);

}
like image 959
Tommy Devoy Avatar asked Oct 07 '22 09:10

Tommy Devoy


1 Answers

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. 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. I’m still using the orientationChanged notifications in my main view’S controller, since it is embedded in the tabBar. But I’m just using autorotation when returning to the main view.

If anyone has any questions, let me know. Hope it helps!

like image 146
Tommy Devoy Avatar answered Oct 10 '22 03:10

Tommy Devoy