首先在头文件中需要提供
协议:代理协议
方法:方便的初始化方法,显示和隐藏方法
属性:代理以及用于tableviewcell点击回调的block
#import <UIKit/UIKit.h>
@class MOBPopover;
@protocol MOBPopoverDelegate <NSObject>
@optional
// 代理方法,用于通知是否已显示
- (void)popViewDidShow:(MOBPopover *)popView;
- (void)popViewDidDismiss:(MOBPopover *)popview;
@end
@interface MOBPopover : UIImageView
@property (nonatomic, weak) id delegate;
@property (nonatomic, copy) void (^selectRowAtIndex)(NSInteger index);
- (instancetype)initWithButton:(UIButton *)button titles:(NSArray *)titles images:(NSArray *)images;
- (void)show;
- (void)dismiss;
@end
主文件
#import "MOBPopover.h"
#define ROW_HEIGHT 44.f
#define SPACE 7
@interface MOBPopover () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, weak) UIButton *cover;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) UIButton *showButton;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageArray;
@end
@implementation MOBPopover
// lazy
- (UITableView *)tableView
{
if (_tableView) {
return _tableView;
}
// 创建tableview
CGRect rect = CGRectMake(SPACE, 13, self.width - SPACE * 2, self.titleArray.count * ROW_HEIGHT);
self.tableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
// tableview属性设置
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.showsHorizontalScrollIndicator = NO;
_tableView.alwaysBounceVertical = NO;
_tableView.alwaysBounceHorizontal = NO;
_tableView.scrollEnabled = NO;
_tableView.backgroundColor = [UIColor clearColor];
return _tableView;
}
- (instancetype)initWithButton:(UIButton *)button titles:(NSArray *)titles images:(NSArray *)images
{
self = [super init];
if (self) {
// 赋值
self.titleArray = titles;
self.imageArray = images;
self.showButton = button;
// 初始化设置
self.image = [UIImage imageNamed:@"popover_background"];
// 添加交互功能,这样添加到上面的tableview才可以点击
self.userInteractionEnabled = YES;
// 设置灰色背景部分的frame
self.width = 217;
self.height = self.titleArray.count * ROW_HEIGHT + SPACE * 3;
// 转换坐标系,将按钮的frame 转换为 相对于整个窗口的绝对坐标
CGRect newRect = [self.showButton convertRect:self.showButton.bounds toView:nil];
self.y = CGRectGetMaxY(newRect);
self.centerX = CGRectGetMidX(newRect);
// 添加tableview
[self addSubview:self.tableView];
}
return self;
}
- (void)show
{
if ([self.delegate respondsToSelector:@selector(popViewDidShow:)]) {
[self.delegate popViewDidShow:self];
}
// 添加一个透明的全屏背景
UIWindow *topWindow = [[[UIApplication sharedApplication] windows] lastObject];
UIButton *coverView = [[UIButton alloc] initWithFrame:topWindow.bounds];
coverView.backgroundColor = [UIColor clearColor];
// 当点击背景时 dismiss
[coverView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[topWindow addSubview:coverView];
[coverView addSubview:self];
self.cover = coverView;
}
-(void)dismiss
{
if ([self.delegate respondsToSelector:@selector(popViewDidDismiss:)]) {
[self.delegate popViewDidDismiss:self];
}
[self.cover removeFromSuperview];
}
#pragma mark - UITableView DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_titleArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.backgroundView = [[UIView alloc] init];
if ([_imageArray count] == [_titleArray count]) {
cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
}
cell.textLabel.font = [UIFont systemFontOfSize:16];
cell.textLabel.text = [_titleArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld", indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (self.selectRowAtIndex) {
self.selectRowAtIndex(indexPath.row);
}
[self dismiss];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ROW_HEIGHT;
}
@end
使用
/**
* 显示popover
*
* @param button 目标按钮
*/
- (void)titleButtonClicked:(UIButton *)button
{
// 创建
MOBPopover * popover = [[MOBPopover alloc] initWithButton:button
titles:@[@"分组一", @"分组二", @"分组三"]
images:@[@"timeline_icon_comment.png",@"timeline_icon_unlike.png",@"timeline_icon_retweet.png"]];
// 设置回调代码块
popover.selectRowAtIndex = ^(NSInteger index){
NSLog(@"select index:%ld", index);
};
// 显示
[popover show];
popover.delegate = self;
}