Question

What I need is simple: I want my UICollectionView frame to always fill the screen no matter how the device is rotated.

Right now, I am accomplishing this by using the method "willAnimateRotationToInterfaceOrientation:..." and calling a method that re-creates the collection view on rotation. However, this results in a flicker of the view on rotation as the old view is removed and the new one is added.

So I know I need to use autolayout or struts and springs and I'd like to use the latter. I know how to set struts and springs using the interface builder but the project I am working on is not using nibs or storyboards.

Here is my method for creating the collection view (it is called in viewDidLoad), including the code I have that isn't working:

 - (void)sceneSetup
 {
     CGFloat cvWidth;
     CGFloat cvHeight;

 if ([self getCurrentInterfaceOrientation] || [self getCurrentDeviceOrientation]) {
     self.orientationIsLandscape = YES;
     cvWidth = 1024;
     cvHeight = 768;

 }   else {
        self.orientationIsLandscape = NO;
        cvWidth = 768;
        cvHeight = 1024;
    }

     EmailsLayout *layout = [[EmailsLayout alloc]init];
     [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
     self.emailsCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, cvWidth, cvHeight) collectionViewLayout:layout];

     //here is my attempt to programmatically set struts and springs
     self.view.autoresizesSubviews = YES;
     self.emailsCollectionView.autoresizingMask = (UIViewAutoresizingFlexibleWidth & 
         UIViewAutoresizingFlexibleHeight & UIViewAutoresizingFlexibleLeftMargin & 
          UIViewAutoresizingFlexibleRightMargin & UIViewAutoresizingFlexibleTopMargin & 
           UIViewAutoresizingFlexibleBottomMargin); 

     self.emailsCollectionView.dataSource = self;
     self.emailsCollectionView.delegate = self;
     [self.emailsCollectionView setScrollEnabled:YES];
     self.emailsCollectionView.userInteractionEnabled = YES;
     self.emailsCollectionView.pagingEnabled = YES;
     self.emailsCollectionView.backgroundColor = UIColorFromRGB(0x262d32);
     [self.emailsCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifierEmails];
     [self.emailsCollectionView reloadData];

     [self.view addSubview:self.emailsCollectionView];
}

Not sure what I am doing wrong. Any help is appreciated.

Was it helpful?

Solution

Try using or | to combine multiple autoresisingMask values together instead of and &.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top