修改IOS中UISearchBar的取消按鈕背景、搜尋內容輸入文字框背景和UISearchBar的背景

來源:互聯網
上載者:User

標籤:ios   uisearchbar   修改取消按鈕背景   修改uisearchbar背景   

 轉載請標明出處:http://blog.csdn.net/android_ls/article/details/39993433

測試的手機IOS系統版本號碼為:6.1.3,實現步驟如下:

1、添加UISearchBar到父View

    _searchBar = [[UISearchBar alloc]init];    _searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, kSeachBarH);    _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;    _searchBar.delegate = self;    _searchBar.placeholder = @"請輸入姓名、公司名稱、公司產品名稱";    [self.view addSubview:_searchBar];
2、修改搜尋方塊背景
    UIImage *img = [UIImage resizedImage:@"find_bg.png"];    [_searchBar setBackgroundImage:img];
3、修改搜尋輸入框內左側的指示表徵圖

    [_searchBar setImage:[UIImage resizedImage:@"ic_search.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

4、修改搜尋輸入文本的背景

    [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"login_btn_input_side.png"] forState:UIControlStateNormal];

     註:對於設計人員提供的搜尋輸入文本的背景,若提供的是一個圓角的小方塊,按常理我們會使用展開圖片的中間部分的方法,經測試顯示效果如下:


     若讓設計人員重新提供一張固定高度的圖片(比如高是60),當做搜尋輸入文本的背景,如下:


5、修改UISearchBar右側的取消按鈕文字顏色及背景圖片

#pragma mark 搜尋方塊的代理方法,搜尋輸入框獲得焦點(聚焦)-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{    [searchBar setShowsCancelButton:YES animated:YES];    // 修改UISearchBar右側的取消按鈕文字顏色及背景圖片    for (UIView *searchbuttons in [searchBar subviews]){        if ([searchbuttons isKindOfClass:[UIButton class]]) {            UIButton *cancelButton = (UIButton*)searchbuttons;            // 修改文字顏色            [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];            [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];                        // 修改按鈕背景            [cancelButton setBackgroundImage:[UIImage resizedImage:@"login_btn_login.png"] forState:UIControlStateNormal];            [cancelButton setBackgroundImage:nil forState:UIControlStateHighlighted];        }    }}
註:修改取消按鈕文字顏色及背景圖片的程式碼片段,一定要放到取消按鈕會顯示代理方法中修改,否則遍曆找不著呀,那就修改不了了。



修改IOS中UISearchBar的取消按鈕背景、搜尋內容輸入文字框背景和UISearchBar的背景

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.