• iOS搜索框


    1. UISearchBar 是UIKit提供的最简单,底层的一个控件,创建它:

     UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];
     searchBar.delegate=self;
     [self.view addSubview:searchBar];

    他有一个代理UISearchBarDelegate,代理方法里常用的是这个:

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;

    2. UISearchDisplayController 这个iOS 8后就被抛弃了,它其实是在UISearchBar上封装了一下,创建它:

        UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        //[self.view addSubview:searchBar];
        table.tableHeaderView = searchBar;
        UISearchDisplayController *searchController = [[UISearchDisplayController alloc]initWithSearchBar:searchBar                                       contentsController:self];
        searchController.delegate = self;
        searchController.searchResultsDataSource = self;

    它有两个代理:UISearchDisplayDelegate,searchResultsDataSource。常用代理方法:

    //在搜索关键字发生改变时调用
    -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(nullable NSString *)searchString{
        return YES;
    }
    
    //在搜索表视图加载完毕后调用
    -(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView{
    
    }

    3. SecondViewController 这个是最新的搜索框,它也是基于UISearchBar封装的,创建它:

        ///搜索模块
        SecondViewController *searchResultVC = [SecondViewController new];
        UISearchController *searchController = [[UISearchController alloc]initWithSearchResultsController:searchResultVC];
        searchController.delegate = self;
        searchController.searchResultsUpdater = self;
        searchController.searchBar.frame = CGRectMake(0, 64, self.view.frame.size.width, 44);
        //table.tableHeaderView = searchController.searchBar;
        [self.view addSubview:searchController.searchBar];
        //searchController.dimsBackgroundDuringPresentation = NO;
        [searchController.searchBar sizeToFit];
        //搜索框颜色
        searchController.searchBar.barTintColor = [UIColor orangeColor];
        //searchController.definesPresentationContext=YES;

    它的代理:UISearchControllerDelegate,UISearchResultsUpdating 。常用代理方法:

    //要显示搜索结果,你必须手动刷新表视图
    -(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    }

    想写一个类似iphone系统通讯录的Demo,它有一个可以上推的搜索框,借鉴了一下别人的代码(地址:https://github.com/ChenYilong/CYLSearchViewController )实现步骤简单记录一下:

    1. 可以用代码,也可以用storyboard

    代码方式:navigationController是AppDelegate的一个导航栏控制器属性

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        
        if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
            [UINavigationBar appearance].tintColor = [UIColor redColor];
            [[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName : [UIColor yellowColor]}];
            [[UINavigationBar appearance] setBarTintColor:
             [UIColor colorWithRed:22/255.0 green:59/255.0 blue:188/255.0 alpha:1]
             ] ;
            [[UINavigationBar appearance] setTranslucent:NO];
        }
        self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor=[UIColor whiteColor];
        
        RootViewController *rootVC=[[RootViewController alloc]init];
        self.navigationController=[[UINavigationController alloc]initWithRootViewController:rootVC];
        //rootVC.view.backgroundColor = [UIColor orangeColor];
        //[self.window addSubview:rootVC.view];
        self.window.rootViewController=self.navigationController;
        
        [self.window makeKeyAndVisible];
        
        return YES;
    }

    storyboard方式:要重写一下导航栏控制器或者在RootViewController中也行,主要是给AppDelegate的navigationController初始化一下值,因为后面要用到。

    我这里自定义导航栏控制器中做的:

        //给AppDelegate的navigationController属性赋值(使用storyboard必须写)
        AppDelegate *delegate = ((AppDelegate *)[[UIApplication sharedApplication] delegate]);
        delegate.navigationController=self;

    2. 然后就是在RootViewController中 写一个UISearchBar, 实现代理方法,在这个代理方法写让搜索框上推功能:

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
        NSLog(@"called when text starts editing");
        CYLSearchController *controller = [[CYLSearchController alloc] initWithNibName:@"CYLSearchController" bundle:nil];
        controller.delegate = self;
        self.searchController = [[UINavigationController alloc] initWithRootViewController:controller];
        [controller showInViewController:self];
    
    }

    这样就差不多完成了,详细实现可以参考他的代码。

    我的通讯录Demo地址:https://github.com/nwgdegitHub/-iphoneAddressList (待完善)

    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935. 我的gitHub: (学习代码都在gitHub) https://github.com/nwgdegitHub/
  • 相关阅读:
    第二次冲刺-个人总结01
    构建之法阅读笔记03
    第一次冲刺-个人总结07
    第十四周总结
    第一次冲刺-个人总结07
    第一次冲刺-个人总结06
    第一次冲刺-个人总结05
    mysql优化
    springmvc常用注解标签详解
    弄懂JDK、JRE和JVM到底是什么
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/7490573.html
Copyright © 2020-2023  润新知