UISearchBar-tableHeaderView

關(guān)于UISearchBar在tableHeaderView的情況

self.tableView.tableHeaderView = self.searchBar;
- (UISearchBar *)searchBar {
    if (_searchBar == nil)
    {
        _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-80, 40)];
        _searchBar.placeholder = @"搜索";
        _searchBar.searchBarStyle = 0;
        _searchBar.delegate = self;
// 邊框顏色
        _searchBar.backgroundImage = [self createImageWithColor:kRGBA(242, 242, 242, 1)];
        UITextField *searchField = [_searchBar valueForKey:@"searchField"];
        if (searchField) {
            [searchField setBackgroundColor:[UIColor whiteColor]];
            [searchField setValue:kGrayColor forKeyPath:@"_placeholderLabel.textColor"];
            // 修正光標(biāo)顏色
            [searchField setTintColor:kGrayColor];
        }
    }
    return _searchBar;
}

#pragma mark - 根據(jù)顏色獲取圖片
- (UIImage *)createImageWithColor:(UIColor *)color
{
    //圖片尺寸
    CGRect rect = CGRectMake(0, 0, 10, 10);
    //填充畫筆
    UIGraphicsBeginImageContext(rect.size);
    //根據(jù)所傳顏色繪制
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    //顯示區(qū)域
    CGContextFillRect(context, rect);
    // 得到圖片信息
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    //消除畫筆
    UIGraphicsEndImageContext();
    return image;
}
代理方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    //隱藏取消按鈕
    searchBar.showsCancelButton = NO;

    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    // 當(dāng)text改變 就會(huì)調(diào)用
}
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,282評(píng)論 3 38
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 1.獲取bean的方式 1.BeanFactory有很多實(shí)現(xiàn)類,通常使用org.springframework.b...
    jihe_lunlixue閱讀 482評(píng)論 0 0
  • 有不少初學(xué)者問到,我想學(xué)習(xí)后端,但是又不知道該怎么學(xué),所以我決定把這幾年的經(jīng)驗(yàn)和經(jīng)歷整理成一篇文章,分析后端的路該...
    yubang閱讀 32,900評(píng)論 30 94
  • 每一個(gè)孩子本身的程序都是自熱而然的完美的程序,但如果基礎(chǔ)語(yǔ)言被改變,不斷地受到病毒的攻擊,就會(huì)讓孩子的發(fā)展方向發(fā)生...
    孫老師說閱讀 715評(píng)論 0 0

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