0

I've subclassed a UINavigationController by adding a UICollectionViewController as a child view controller when I init the subclass:

-(id)init{
    self = [super init];
    if(self){

        UICollectionViewController *collectionViewController = [[UICollectionViewController alloc] initWithCollectionViewLayout:[self getLayout]];

        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPressed)];
        collectionViewController.navigationItem.rightBarButtonItem = cancelItem;

        [self addChildViewController:collectionViewController];
    }
    return self;
} 

To initialize the layout I call the getLayout method:

- (UICollectionViewFlowLayout *)getLayout {

    CGFloat itemWidth = self.view.frame.size.width;

    CGSize itemSize = CGSizeMake(itemWidth, itemWidth);
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setItemSize:itemSize];
    return flowLayout;
}

However, when I use this code no navigation bar items are shown for iOS 7 apps (iOS 8 still works). However, if I change the line:

 CGFloat itemWidth = self.view.frame.size.width;

to

 CGFloat itemWidth = [[UIScreen mainScreen] bounds].size.width;

All works well. Both lines return the same value. Any ideas why this is happening?

Jbryson
  • 2,875
  • 1
  • 31
  • 53
  • Try adding your views in the viewDidLoad method. Its very likely that you are configuring your controller before its view has been loaded. – Daniel Galasko Oct 09 '14 at 15:58
  • It makes no difference. I have no problem using the screen size, I was just wondering why it makes a difference. – Jbryson Oct 09 '14 at 16:50

0 Answers0