【iOS干貨:快速集成搜索界面下拉菜單框架】

前言

這次推出的控件,比較常用,搜索界面下拉菜單,如果喜歡我的文章,可以關(guān)注我微博:袁崢Seemygo

Demo效果:

效果圖.gif

Demo演示:

1.創(chuàng)建下拉菜單

    YZPullDownMenu *menu = [[YZPullDownMenu alloc] init];
    menu.frame = CGRectMake(0, 20, YZScreenW, 44);
    [self.view addSubview:menu];

2.設(shè)置下拉菜單代理

menu.dataSource = self;

3.添加所有下拉菜單對(duì)應(yīng)的子控制器

為什么要這樣設(shè)計(jì)?,因?yàn)槊總€(gè)app對(duì)應(yīng)的下拉菜單不確定,所以交給各個(gè)開(kāi)發(fā)者決定,下拉菜單的界面。

- (void)setupAllChildViewController
{
    YZAllCourseViewController *allCourse = [[YZAllCourseViewController alloc] init];
    YZSortViewController *sort = [[YZSortViewController alloc] init];
    YZMoreMenuViewController *moreMenu = [[YZMoreMenuViewController alloc] init];
    // 控制器最好作為自己的子控制器
    [self addChildViewController:allCourse];
    [self addChildViewController:sort];
    [self addChildViewController:moreMenu];
}

4.實(shí)現(xiàn)YZPullDownMenu數(shù)據(jù)源方法

#pragma mark - YZPullDownMenuDataSource
// 返回下拉菜單多少列
- (NSInteger)numberOfColsInMenu:(YZPullDownMenu *)pullDownMenu
{
    return 3;
}

// 返回下拉菜單每列按鈕
- (UIButton *)pullDownMenu:(YZPullDownMenu *)pullDownMenu buttonForColAtIndex:(NSInteger)index
{
    YZMenuButton *button = [YZMenuButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:_titles[index] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:25 /255.0 green:143/255.0 blue:238/255.0 alpha:1] forState:UIControlStateSelected];
    [button setImage:[UIImage imageNamed:@"標(biāo)簽-向下箭頭"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"標(biāo)簽-向上箭頭"] forState:UIControlStateSelected];
    
    return button;
}

// 返回下拉菜單每列對(duì)應(yīng)的控制器
- (UIViewController *)pullDownMenu:(YZPullDownMenu *)pullDownMenu viewControllerForColAtIndex:(NSInteger)index
{
    return self.childViewControllers[index];
}

// 返回下拉菜單每列對(duì)應(yīng)的高度
- (CGFloat)pullDownMenu:(YZPullDownMenu *)pullDownMenu heightForColAtIndex:(NSInteger)index
{
    // 第1列 高度
    if (index == 0) {
        return 400;
    }
    
    // 第2列 高度
    if (index == 1) {
        return 180;
    }
    
    // 第3列 高度
    return 240;
}

5.【更新菜單標(biāo)題,需要發(fā)送通知給我】

為什么要這樣設(shè)計(jì)?解耦,自己的控制器中就不需要導(dǎo)入我的框架的頭文件了,侵入性不大。

【更新菜單標(biāo)題步驟】

  • 1.把 【extern NSString * const YZUpdateMenuTitleNote;】這行代碼拷貝到自己控制器中,這個(gè)在YZPullDownMenu.h

  • 2.在選中標(biāo)題的方法中,發(fā)送以下通知
    [[NSNotificationCenter defaultCenter] postNotificationName:YZUpdateMenuTitleNote object:self userInfo:@{@"title":cell.textLabel.text}];

  • 3.1 postNotificationName:通知名稱 =>【YZUpdateMenuTitleNote】

  • 3.2 object:誰(shuí)發(fā)送的通知 =>【self】(當(dāng)前控制器)

  • 3.3 userInfo:選中標(biāo)題信息 => 可以多個(gè)key,多個(gè)value,沒(méi)有固定的,因?yàn)橛行┙缑妫枰催x很多選項(xiàng),key可以隨意定義。

  • 3.4 底層會(huì)自動(dòng)判定,當(dāng)前userInfo有多少個(gè)value,如果有一個(gè)就會(huì)直接更新菜單標(biāo)題,有多個(gè)就會(huì)更新,滿足大部分需求。

  • 3.5 發(fā)出通知,會(huì)自動(dòng)彈回下拉菜單

5.1 可以參考YZSortViewController中代碼

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectedCol = indexPath.row;
    
    // 選中當(dāng)前
    YZSortCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // 更新菜單標(biāo)題
    [[NSNotificationCenter defaultCenter] postNotificationName:YZUpdateMenuTitleNote object:self userInfo:@{@"title":cell.textLabel.text}];
}

源碼

點(diǎn)擊這下載源代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,039評(píng)論 4 61
  • 前言 這個(gè)控件比較常用,至少目前公司三個(gè)項(xiàng)目都用到了這個(gè)下拉菜單,是時(shí)候封裝分享一下了。覺(jué)的不錯(cuò)的麻煩點(diǎn)個(gè)喜歡,三...
    so_what閱讀 4,122評(píng)論 3 17
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,741評(píng)論 25 709
  • meterpreter是更高級(jí)的payload。 純內(nèi)存工作模式(不寫(xiě)硬盤(pán)),避免被電子取證;加密的通信協(xié)議,對(duì)傳...
    Instu閱讀 6,579評(píng)論 0 2
  • 拿到新辦公室的鑰匙有點(diǎn)蒙圈閉上眼睛有無(wú)數(shù)的需求跳了出來(lái)有無(wú)數(shù)需要縷順的 借阿桃的圖鼓勵(lì)一下自己 晚安 全世界#
    哈哈同學(xué)閱讀 168評(píng)論 0 0

友情鏈接更多精彩內(nèi)容