将 UICollectionView 添加到 NavigationBar
Add UICollectionView to NavigationBar
我正在尝试将 UICollectionView 作为子视图添加到我的 NavigationBar。
[self.navigationController.view addSubview:self.hotspotCollectionView];
它似乎工作正常但覆盖了后退按钮,请参见所附的屏幕截图。无论如何缩进 collectionView 以便后退按钮正确可见?还有没有办法增加导航栏的高度,以便我可以在 CollectionView 中使用更大的缩略图?
我正在使用 XCode 7 和 iOS9。
我认为在这种情况下最好的决定是使用模仿 UINavigationBar
的自定义 UIView
子类。而setNavigationBarHidden:YES animated:NO
对应UINavigationController
.
我能够实现这个简单的例子
通过使用 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: 我不能保证这是一个面向未来的解决方案或在任何情况下都有效。我没有用自动布局或旋转之类的东西测试它。
免责声明 2: 我个人会独立于视图控制器实现任何 datasource/delegate。对于 tableViews 和 collection views,我使用我自己开发的 OFAPopulator.
这里有一个示例代码:gitlab.com/vikingosegundo/collectionview-in-navigationbar/tree/…里面还有一些UI怪癖。但这可能是意料之中的。我猜苹果只是没有考虑我们改变高度。实际上,我从未见过导航栏高度不同的应用程序。但是代码回答了你的问题。给它更多的爱,它可能对你有用。
我正在尝试将 UICollectionView 作为子视图添加到我的 NavigationBar。
[self.navigationController.view addSubview:self.hotspotCollectionView];
它似乎工作正常但覆盖了后退按钮,请参见所附的屏幕截图。无论如何缩进 collectionView 以便后退按钮正确可见?还有没有办法增加导航栏的高度,以便我可以在 CollectionView 中使用更大的缩略图?
我正在使用 XCode 7 和 iOS9。
我认为在这种情况下最好的决定是使用模仿 UINavigationBar
的自定义 UIView
子类。而setNavigationBarHidden:YES animated:NO
对应UINavigationController
.
我能够实现这个简单的例子
#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: 我不能保证这是一个面向未来的解决方案或在任何情况下都有效。我没有用自动布局或旋转之类的东西测试它。
免责声明 2: 我个人会独立于视图控制器实现任何 datasource/delegate。对于 tableViews 和 collection views,我使用我自己开发的 OFAPopulator.
这里有一个示例代码:gitlab.com/vikingosegundo/collectionview-in-navigationbar/tree/…里面还有一些UI怪癖。但这可能是意料之中的。我猜苹果只是没有考虑我们改变高度。实际上,我从未见过导航栏高度不同的应用程序。但是代码回答了你的问题。给它更多的爱,它可能对你有用。