DJHomeViewController.m
/** 设置导航栏左侧内容 */ - (void)setupLeftNavItem { // Logo UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_meituan_logo"]]; UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithCustomView:logoView]; // 类别 DJNavItem *categoryView = [DJNavItem item]; [categoryView addTarget:self action:@selector(onCategoryNavItemDidClick)]; UIBarButtonItem *categoryItem = [[UIBarButtonItem alloc] initWithCustomView:categoryView]; self.categoryItem = categoryItem; // 地区 DJNavItem *areaView = [DJNavItem item]; [areaView addTarget:self action:@selector(onAreaNavItemDidClick)]; UIBarButtonItem *areaItem = [[UIBarButtonItem alloc] initWithCustomView:areaView]; self.areaItem = areaItem; // 排序 DJNavItem *sortView = [DJNavItem item]; [sortView addTarget:self action:@selector(onSortNavItemDidClick)]; UIBarButtonItem *sortItem = [[UIBarButtonItem alloc] initWithCustomView:sortView]; self.sortItem = sortItem; self.navigationItem.leftBarButtonItems = @[logoItem,categoryItem,areaItem,sortItem]; } #pragma mark - 导航按钮被点击 - (void)onCategoryNavItemDidClick { // 创建将要显示的Controller DJCategoryViewController *categoryVC = [[DJCategoryViewController alloc] init]; // 设置控制器View的显示大小 categoryVC.preferredContentSize = CGSizeMake(300, 360); // 设置Modal类型 categoryVC.modalPresentationStyle = UIModalPresentationPopover; // 获取Popover UIPopoverPresentationController *categoryPopover = categoryVC.popoverPresentationController; // 相对于哪个View来显示 categoryPopover.sourceView = self.categoryItem.customView; // 显示位置 categoryPopover.sourceRect = self.categoryItem.customView.bounds; // 设置popover箭头的显示方向 categoryPopover.permittedArrowDirections = UIPopoverArrowDirectionAny; // 跳转 [self presentViewController:categoryVC animated:YES completion:nil]; }
DJNavDropView.m
#import "DJNavDropView.h" #import "DJCategory.h" @interface DJNavDropView()<UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *mainTableView; @end @implementation DJNavDropView + (instancetype)dropView { return[[[NSBundle mainBundle] loadNibNamed:@"DJNavDropView" owner:nil options:nil] lastObject]; } - (void)setCategoryList:(NSArray *)categoryList { _categoryList = categoryList; // 刷新数据 [self.mainTableView reloadData]; } #pragma mark - TableView 数据源方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.categoryList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"category"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } DJCategory *categoryItem = self.categoryList[indexPath.row]; cell.textLabel.text = categoryItem.name; cell.imageView.image = [UIImage imageNamed:categoryItem.icon]; return cell; } @end
最终效果:
原文:http://www.cnblogs.com/yongdaimi/p/6250405.html