自定义搜索栏

iDe*_*per 1 xcode objective-c uisearchbar ios

我正在设计一个搜索栏。我需要搜索栏看起来像图像中的一个。尝试了几种方法,但没有改变。非常感谢帮助。

截屏

[已编辑] 使用 Richard 的代码后查看图像,它看起来像这样。我希望灰色是清晰的。

截屏

Mah*_*dra 5

self.searchBar是一个IBOutlet。您也可以动态创建它。

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;
Run Code Online (Sandbox Code Playgroud)

输出 :

在此处输入图片说明