美文网首页
UISearchBar

UISearchBar

作者: 任性不认命ToT | 来源:发表于2016-08-03 10:43 被阅读0次

    一、UISearchBar的介绍



    二、UISearchBar 的常用属性

    下面直接用一个例子,阐述UISearchBar的一些属性和设置

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(20, 20, SCREEN_WIDTH - 40, 40)];
    /** 设置搜索框上一些小控件的图片,状态为UIControlStateNormal和UIControlStateDisabled
     UISearchBarIconSearch          搜索
     UISearchBarIconClear           清除
     UISearchBarIconBookmark        书
     UISearchBarIconResultsList     结果
     tip:图片均使用原始尺寸,所以尺寸需要注意
     */
    [searchBar setImage:[UIImage imageNamed:@""] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    searchBar.delegate = self;
    /** 设置搜索框的类型 */
    searchBar.barStyle = UIBarStyleDefault;
    /** 显示在顶部的一小行文字 */
    searchBar.prompt = @"1111";
    /** 在未输入文字时显示的标识字符 */
    searchBar.placeholder = @"这是一个搜索框";
    
    /** 显示书,取消,结果按钮 */
    searchBar.showsBookmarkButton = YES;
    searchBar.showsCancelButton = YES;
    searchBar.showsSearchResultsButton = YES;
    
    /** 设置搜索框辅助选择框显示,并设置标题 */
    searchBar.showsScopeBar = YES;
    searchBar.scopeButtonTitles = @[@"scopeButton1", @"scopeButton2"];
    /** 设置搜索框辅助选择框的背景图片和文字属性 */
    searchBar.scopeBarBackgroundImage = [UIImage imageWithColor:[UIColor greenColor]];
    [searchBar setScopeBarButtonDividerImage:[UIImage imageWithColor:[UIColor redColor]] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
    [searchBar setScopeBarButtonTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor], NSFontAttributeName : [UIFont systemFontOfSize:10]} forState:UIControlStateNormal];
    [searchBar setScopeBarButtonTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:20]} forState:UIControlStateSelected];
    [self.view addSubview:searchBar];
    

    示例运行结果

    三、UISearchBar代理

    #pragma mark - delegate
    /** 还有一些输入框的文本变化代理,就不一一罗列了 */
    /** 点击书 */
    - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"bookmark");
    }
    /** 点击取消按钮 */
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"cancelButton");
        [self.searchBar resignFirstResponder];
    }
    /** 点击结果按钮 */
    - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"resultButton");
    }
    /** 点击辅助选择按钮 */
    - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
        NSLog(@"scope:%li", selectedScope);
    }
    

    相关文章

      网友评论

          本文标题:UISearchBar

          本文链接:https://www.haomeiwen.com/subject/gyyfsttx.html