将UICollectionView添加到NavigationBar

我想添加一个UICollectionView作为一个子视图到我的NavigationBar。

[self.navigationController.view addSubview:self.hotspotCollectionView]; 

它似乎正常工作,但覆盖后退button,请参阅附加的屏幕截图。 有无论如何缩进collectionView,以便返回button是正确的可见? 还有一种方法来增加导航栏的高度,所以我可以在我的CollectionView中使用更大的缩略图图片?

我正在使用XCode 7和iOS9。

NavigationBar中的CollectionView

我能够实现这个简单的例子

截图

在这里输入图像说明 通过使用UINavigationBar的一个子类,并设置集合视图到视图控制器的navigationItem.titleView


 #import <UIKit/UIKit.h> @interface NavigationBar : UINavigationBar @end 

 #import "NavigationBar.h" @implementation NavigationBar - (CGSize)sizeThatFits:(CGSize)size { return CGSizeMake(self.superview.bounds.size.width, 80); } -(void)setFrame:(CGRect)frame { CGRect f = frame; f.size.height = 80; [super setFrame:f]; } @end 

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end 

 #import "ViewController.h" #import "CollectionViewCell.h" @interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> @property (nonatomic, weak) UICollectionView *collectionView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 300, 80) collectionViewLayout:layout]; collectionView.backgroundColor = [UIColor clearColor]; self.navigationItem.titleView = collectionView; [collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; collectionView.delegate = self; collectionView.dataSource = self; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 5; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor orangeColor]; return cell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(50, 50); } @end 

我在Interface Builder中设置了自定义的NavigationBar

截图


免责声明1:我不能保证这是一个未来的certificate解决scheme,或将在任何情况下工作。 我没有用自动布局或旋转的东西来testing它。

免责声明2:我个人会独立于视图控制器实现任何数据源/委托。 对于tableViews和集合视图,我使用我自己生长的OFAPopulator 。


这里是一个示例代码:gitlab.com/vikingosegundo/collectionview-in-navigationbar/tree/ …还有一些用户界面的怪癖。 但可能这是可以预料的。 我想苹果公司并没有考虑改变我们的身高。 其实我从来没有看到一个不同高度的应用程序的导航栏。 但是代码回答你的问题。 给它更多的爱,这可能对你有用。

我认为在这种情况下最好的决定是使用模仿UINavigationBar自定义UIView子类。 并将setNavigationBarHidden:YES animated:NO到相应的UINavigationController