先上系统默认的UISearchBar,然后用KVO修改

UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:_topView.bounds];
searchBar.placeholder = @"请输入您要搜索的商品";

效果为:

①将取消按钮cancle改为“取消”

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
searchBar.showsCancelButton = YES;
for(UIView *view in [[[searchBar subviews] objectAtIndex:] subviews]) {
if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
UIButton * cancel =(UIButton *)view;
[cancel setTitle:@"取消" forState:UIControlStateNormal];
[cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
cancel.titleLabel.font = [UIFont systemFontOfSize:];
}
}
}

效果为

②修改searchbar背景色

UIView *backView = [[UIView alloc]initWithFrame:searchBar.bounds];
backView.backgroundColor = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
[searchBar insertSubview:backView atIndex:];

③设置searchbar输入框背景颜色及删减符号mode

UITextField *searchField = [searchBar valueForKey:@"_searchField"];
searchField.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
searchField.clearButtonMode = UITextFieldViewModeNever;

④修改searchbar输入框的文字和占位符文字颜色

UITextField *searchField = [searchBar valueForKey:@"_searchField"];
searchField.textColor = [UIColor whiteColor];
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];

⑤修改放大镜图标

UITextField *searchField = [searchBar valueForKey:@"_searchField"];
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_search"]];
iconView.frame = CGRectMake(, , , );
searchField.leftView = iconView;

自定义UISearchBar的更多相关文章

  1. iOS 如何自定义UISearchBar 中textField的高度

    iOS 如何自定义UISearchBar 中textField的高度 只需设置下边的方法就可以 [_searchBar setSearchFieldBackgroundImage:[UIImage i ...

  2. 新浪微博客户端(5)-自定义UISearchBar

    iOS自带的UISearchBar有很多限制,我们可以使用UITextField做出一个类似于SearchBar的效果. //===================================== ...

  3. 自定义UISearchBar外观

    1. 设置背景色 我以白色的背景色为例,下面看看代码: //1. 设置背景颜色 //设置背景图是为了去掉上下黑线 self.customSearchBar.backgroundImage = [[UI ...

  4. iOS 将navigationItem.titleView设置为自定义UISearchBar (Ficow实例讲解)

    这篇文章可以解决以下问题: 1.将searchBar设置为titleView后,无法调整位置的问题 : 2.searchBar的背景色无法设置为透明色的问题: 3.searchBar输入框内用户输入的 ...

  5. iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)

    很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...

  6. UISearchBar 自定义处理

    首先通过 KVC 获取到内部的 textField, 然后自定制处理 UITextField *searchField = [searchBar valueForKey:@"searchFi ...

  7. IOS开发之UISearchBar自定义外观

      MySearchBar.h如下: @interface MySearchBar : UISearchBar - (void)layoutSubviews; @end MySearchBar.m如下 ...

  8. UIsearchBar 自定义样式

    对于修改输入框圆角 的需求, 可以通过修改搜索框背景图片的方式设置. - (void)setSearchFieldBackgroundImage:(nullable UIImage *)backgro ...

  9. iOS数字键盘自定义按键

    UIKeyboardTypeNumberPad 数字键盘自定义按键 最近做一个搜索用户的功能,这里使用了UISearchBar.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: ...

随机推荐

  1. AX7: HOW TO USE TABLE METHOD EXTENSION CLASS

    To create new methods on a table without customize you should use the Table method extension class. ...

  2. MS AX 技术相关网站收藏

    Microsoft Dynamics AX Developer Centerhttps://msdn.microsoft.com/en-us/dynamics/ax/default.aspx From ...

  3. C#如何分割多个空格分隔的字符串?

    using System; using System.Text; using System.Text.RegularExpressions; namespace test { class Progra ...

  4. backup1

    string _imgpath1 = WebConfigurationManager.AppSettings["IMGPATH1"].ToString(); string outp ...

  5. android开发--ormlite

    ORMlite是类似hibernate的对象映射框架,主要面向java语言,同时,是时下最流行的android面向数据库的的编程工具. 官方网站:http://ormlite.com/ 如果需要开发a ...

  6. promise实例小球运动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Div添加阴影效果

    -moz-box-shadow: 2px 2px 10px #909090;/*firefox*/ -webkit-box-shadow: 2px 2px 10px #909090;/*safari或 ...

  8. 『c++』 模板(template)--- 参数化多态性

    ---恢复内容开始--- 题外话: 模板机制的设计和细节是由Bjarne Stroustrup在其1988年10月发表的名为“Parameterized Types for C++”一文中披露的. 引 ...

  9. android 判断sd的状态,所有文件,剩余空间的大小

    public class MainActivity extends AppCompatActivity { String TAG = MainActivity.class.getCanonicalNa ...

  10. HTTP权威协议笔记-7.集成点:网关、隧道及中继

    .8.1 网关 定义:网关类似与翻译器,它抽象出了一种能够到达资源的方法. 实用:网关可以自动将HTTP流量转换为其他协议,这样使用HTTP协议的一方就不需要了解其他协议,也可实现与其他程序或设备交互 ...