自定义搜索栏 [英] Customize search bar

查看:90
本文介绍了自定义搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am designing a search bar. I need the search bar to look like one in the image. Tried few ways but no changes. Help much appreciated.

[已编辑] 在使用Richard的代码后,看到的图像看起来像这样.我希望灰色清晰.

See the image after using Richard's code it looks like this. I want the gray color to be clear.

推荐答案

self.searchBarIBOutlet.您还可以动态创建它.

self.searchBar is an IBOutlet. You can also create it dynamically.

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;

输出:

这篇关于自定义搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆