設(shè)置UISearchBar背景色柿祈,就調(diào)用barTintColor,發(fā)現(xiàn)無效哩至,可是查了官方文檔和別人的blog,都說這個(gè)屬性就可以躏嚎。后來又讀到以下一段文字:
UISearchBar是由兩個(gè)subView組成的,一個(gè)是UISearchBarBackGround,另一個(gè)是UITextField.
就用于Reveal工具調(diào)試菩貌,才找到原因紧索,原來是UITextField把UISearchBarBackGround蓋住了。示例代碼如下:
ViewController.m
//自定義導(dǎo)航欄
CGFloat navViewH = 44;
UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
// navView.backgroundColor = [UIColor blueColor];
//自定義導(dǎo)航欄左側(cè)按鈕
CGFloat leftImageViewX = 10;
CGFloat leftImageViewW = 15;
CGFloat leftImageViewH = 25;
CGFloat leftImageViewY = (navViewH - leftImageViewH)/2;
UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftImageViewX, leftImageViewY, leftImageViewW, leftImageViewH)];
leftImageView.image = [UIImage imageNamed:@"8-2"];
[navView addSubview:leftImageView];
//自定義中間搜索框
CGFloat searchBarW = kScreenWidth * 3/4;
CGFloat searchBorder = 20;
CGFloat searchBarH = 30;
CGFloat searchBarY = (navViewH - searchBarH)/2;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftImageView.frame) + searchBorder, searchBarY, searchBarW, searchBarH)];
[navView addSubview:searchBar];
searchBar.layer.cornerRadius = 15;
searchBar.clipsToBounds = YES;
searchBar.placeholder = @"請(qǐng)輸入目的地菜谣、地標(biāo)或民宿名稱";
searchBar.barTintColor = [UIColor yellowColor];
//去掉上下邊框黑色
// searchBar.backgroundImage = [[UIImage alloc] init];
self.navigationController.navigationBar.items.lastObject.titleView = navView;
從圖上可以看出珠漂,是SearchFeild遮住了SearchBackgroud
知道了原因就好辦了,用searchBar自帶方法尾膊,解決方案如下:
[searchBar setSearchFieldBackgroundImage:[UIImage createImageWithColor:[UIColor greenColor]] forState:UIControlStateNormal];
Reveal的截圖如下: