自定义 popover

首先在头文件中需要提供

协议:代理协议
方法:方便的初始化方法,显示和隐藏方法
属性:代理以及用于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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 禅与 Objective-C 编程艺术 (Zen and the Art of the Objective-C C...
    GrayLand阅读 1,673评论 1 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,168评论 19 139
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,241评论 30 472
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,262评论 6 13
  • 我有时候觉得能忍的人很厉害。很多时候我不知道该怎么办,即使学了很多东西,即使理解很多事情,人性也始终是个奇怪的东西...
    麻小壹贰叁肆伍陆柒阅读 126评论 0 0