仿手机QQ下拉菜单框架(FFDropDownMenu) -- 自定义菜单样式(非xib)

最近写了这个框架: FFDropDownMenu,类似手机QQ下拉菜单。
github地址:
https://github.com/chenfanfang/FFDropDownMenu
更多的使用方法的demo地址:
https://github.com/chenfanfang/CollectionsOfExample
更多的使用方法的博客地址:
http://www.jianshu.com/notebooks/5552428/latest

若觉得框架自带的菜单样式不够好看,或者布局不是自己想要的效果,那么,你可以自定义菜单选项的cell,自己给每个菜单选项cell添加子控件,自己对其进行任意样式的布局。这篇博客将介绍如何自定义菜单选项cell。

先来看下效果图

仿QQ下拉菜单自定义菜单样式.gif
仿QQ下拉菜单自定义菜单样式.png

框架自带的菜单选项样式是左边是图标,右边是标题,上面的效果图是左边标题,右边图标。你也可以自定义任何样式,添加更多的控件。

<a id="Installation"></a> Installation【安装】

From CocoaPods【使用CocoaPods】

pod  FFDropDownMenu

Manually【手动导入】

  • Drag all source files under floder FFDropDownMenu to your project.【将FFDropDownMenu文件夹中的所有源代码拽入项目中】

  • FFDropDownMenu文件夹里面的文件有

FFDropDownMenuBasedCell.h        FFDropDownMenuBasedCell.m
FFDropDownMenuBasedModel.h       FFDropDownMenuBasedModel.m
FFDropDownMenuCell.h             FFDropDownMenuCell.m
FFDropDownMenuModel.h            FFDropDownMenuModel.m
FFDropDownMenuTriangleView.h     FFDropDownMenuTriangleView.m
FFDropDownMenuView.h             FFDropDownMenuView.m


【开始使用】

先导入头文件

//若使用CocoaPods
#import <FFDropDownMenuView.h>

//若使用手动导入
#import "FFDropDownMenuView.h"

自定义继承于FFDropDownMenuBasedCell类的菜单cell

自定义菜单cell 的.h文件

#import <FFDropDownMenuBasedCell.h>

@interface FFDropDownMenuCustomCell : FFDropDownMenuBasedCell

@end

自定义菜单cell 的.m文件

#import "FFDropDownMenuCustomCell.h"

//model
#import "FFDropDownMenuModel.h"

@interface FFDropDownMenuCustomCell ()

/** 图片 */
@property (weak, nonatomic) UIImageView *customImageView;

/** 标题 */
@property (weak, nonatomic) UILabel *customTitleLabel;

/** 底部分割线 */
@property (nonatomic, weak) UIView *separaterView;
@end

@implementation FFDropDownMenuCustomCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //初始化子控件
        UIImageView *customImageView = [[UIImageView alloc] init];
        customImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:customImageView];
        self.customImageView = customImageView;
        
        UILabel *customTitleLabel = [[UILabel alloc] init];
        customTitleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:customTitleLabel];
        self.customTitleLabel = customTitleLabel;
        
        UIView *separaterView = [[UIView alloc] init];
        separaterView.backgroundColor = [UIColor colorWithRed:25 / 255.0 green:168 / 255.0 blue:243 / 255.0 alpha:0.3];
        [self addSubview:separaterView];
        self.separaterView = separaterView;
    }
    return self;
}

- (void)layoutSubviews { //这个方法的主要任务是进行子控件frame的赋值
    [super layoutSubviews];
    //frame的赋值
    CGFloat separaterHeight = 1; //底部分割线高度
    
    //图片 customImageView
    CGFloat imageViewMargin = 3;
    CGFloat imageViewH = self.frame.size.height - 2 * imageViewMargin;
    self.customImageView.frame = CGRectMake(10, imageViewMargin, imageViewH, imageViewH);
    
    //标题
    CGFloat labelX = CGRectGetMaxX(self.customImageView.frame) + 10;
    self.customTitleLabel.frame = CGRectMake(labelX, 0, self.frame.size.width - labelX, self.frame.size.height - separaterHeight);
    
    //分割线
    self.separaterView.frame = CGRectMake(0, self.frame.size.height - separaterHeight, self.frame.size.width, separaterHeight);
}

/** 重写setMenuModel---对控件进行赋值 */
- (void)setMenuModel:(id)menuModel {
    _menuModel = menuModel;
    
    FFDropDownMenuModel *realMenuModel = (FFDropDownMenuModel *)menuModel;
    self.customTitleLabel.text = realMenuModel.menuItemTitle;
    //给imageView赋值
    self.customImageView.image = [UIImage imageNamed:realMenuModel.menuItemIconName];
}

@end

若自定义了cell,框架自带的模型FFDropDownMenuModel里面的属性不够用,可以自定义一个继承于FFDropDownMenuBasedModel的模型,自己添加属性。本篇博客的例子用框架自带的模型FFDropDownMenuModel是够用的,所以不自定义菜单模型。自定义菜单模型在这篇博客中有介绍http://www.jianshu.com/p/6a42a35ae2db

到此为止,自定义菜单样式就已经完成,接下来就是如何创建菜单了。

获取下拉菜单模型数组


/** 获取下拉菜单模型数组 */
- (NSArray *)getDropDownMenuModelsArray {
    __weak typeof(self)weakSelf = self;
    
    
    //若 模型FFDropDownMenuModel里面的属性不够用,可以自定义继承于FFDropDownMenuBasedModel的模型
    
    //菜单模型0
    FFDropDownMenuModel *menuModel0 = [FFDropDownMenuModel new];
    menuModel0.menuItemTitle = @"Twitter";
    menuModel0.menuItemIconName = @"menu0";
    menuModel0.menuBlock = ^ {
        UIViewController *vc = [UIViewController new];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    
    //菜单模型1
    FFDropDownMenuModel *menuModel1 = [FFDropDownMenuModel new];
    menuModel1.menuItemTitle = @"Line";
    menuModel1.menuItemIconName = @"menu1";
    menuModel1.menuBlock = ^ {
        //do something
    };
    
    //菜单模型2
    FFDropDownMenuModel *menuModel2 = [FFDropDownMenuModel new];
    menuModel2.menuItemTitle = @"QQ";
    menuModel2.menuItemIconName = @"menu2";
    menuModel2.menuBlock = ^ {
        //do something
    };
    
    NSArray *menuModelArr = @[menuModel0, menuModel1, menuModel2....];
    
    return menuModelArr;
}

创建下拉菜单

- (void)createDropdownMenu {
    NSArray *menuModelsArr = [self getDropDownMenuModelsArray];
    
    self.dropDownMenu = [FFDropDownMenuView new];
    self.dropDownMenu.menuModelsArray = menuModelsArr;
    self.dropDownMenu.cellClassName = @"FFDropDownMenuCustomCell";
    self.dropDownMenu.menuItemBackgroundColor = FFColor(255, 255, 255, 0.7);
    self.dropDownMenu.triangleColor = FFColor(255, 255, 255, 0.7);
    [self.dropDownMenu setup];
}

显示下拉菜单

[self.dropDownMenu showMenu];

期待

  • 如果在使用过程中遇到BUG,希望你能在 简书私信我,或者在我简书专题的博客进行评论。谢谢(或者尝试下载最新的框架代码看看BUG修复没有)
  • 如果在使用过程中发现功能不够用,希望你能在 简书私信我,或者在我简书专题的博客进行评论。我非常想为这个框架增加更多好用的功能,谢谢
  • 如果你想和我一起完善FFDropDownMenu,请Pull Requests我

http://www.jianshu.com/users/80fadb71940d/latest_articles

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容