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?