2016-04-11 158 views
0

我正在设计一个搜索栏。我需要搜索栏在图像中看起来像一个。尝试了几种方式,但没有改变。非常感谢。 enter image description here自定义搜索栏

[编辑] 使用理查兹代码后的图像看起来像这样。我希望灰色清晰。 enter image description here

+0

什么是你的搜索栏样式使用理查兹后码? –

+0

风格默认@ Dev.RK – iDeveloper

回答

2

self.searchBarIBOutlet。您也可以动态创建它。

self.searchBar.layer.borderWidth = 2.0; 
self.searchBar.layer.borderColor = [UIColor brownColor].CGColor; 
self.searchBar.layer.cornerRadius = 15.0; 
self.searchBar.barTintColor = [UIColor colorWithRed:255/255.0 green:246/255.0 blue:241/255.0 alpha:1.0]; 
self.searchBar.backgroundColor = [UIColor clearColor]; 

UITextField *textField = [self.searchBar valueForKey:@"_searchField"]; 
textField.textColor = [UIColor brownColor]; 
textField.placeholder = @"Search"; 
textField.leftViewMode = UITextFieldViewModeNever; //hiding left view 
textField.backgroundColor = [UIColor colorWithRed:255/255.0 green:246/255.0 blue:241/255.0 alpha:1.0]; 
textField.font = [UIFont systemFontOfSize:18.0]; 
[textField setValue:[UIColor brownColor] forKeyPath:@"_placeholderLabel.textColor"]; 

UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)]; 
imgview.image = [UIImage imageNamed:@"searchIcon.png"]; //you need to set search icon for textfield's righ view 

textField.rightView = imgview; 
textField.rightViewMode = UITextFieldViewModeAlways; 

输出:

enter image description here

+0

该代码的作品,但默认搜索栏的灰色仍然存在,因此它看起来不太好。 – iDeveloper

+0

检查更新的图像 – iDeveloper

+0

看到输出..... –

0

你可以用图像来改变UISearchBar appearance-

[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]]; 
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:@"searchBG.png"]]; 
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal]; 
0

你可以使用一些定制的解决方案(这是定制更好) 例如SSSearchBar或试图找到一些类似