缺少带有集合视图子视图控制器的导航栏项

时间:2014-10-09 14:55:58

标签: ios7 uinavigationcontroller uicollectionview uinavigationbar uicollectionviewlayout

我在初始化子类时通过添加UICollectionViewController作为子视图控制器来创建UINavigationController的子类:

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

要初始化布局,我调用getLayout方法:

- (UICollectionViewFlowLayout *)getLayout {

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

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

然而,当我使用此代码时,没有为iOS 7应用程序显示导航栏项目(iOS 8仍然有效)。但是,如果我更改了行:

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

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

一切运作良好。两行都返回相同的值。任何想法为什么会发生这种情况?

0 个答案:

没有答案