自定义搜索栏
我正在设计一个搜索栏。 我需要搜索栏看起来像图像中的一个。 试过几种但没有变化的方法。 非常感谢。
[已编辑]使用Richards代码后查看图像,看起来像这样。 我希望灰色清晰。
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;
输出:
您可以使用图像来更改UISearchBar
外观 –
[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]]; [[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:@"searchBG.png"]]; [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];
您可以使用一些自定义解决方案(它更适合自定义)例如SSSearchBar或尝试查找一些类似的