1、修改UISearchBar的背景颜色

UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性。方法是直接将 UISearchBarBackGround移去

  1. seachBar=[[UISearchBar alloc] init];
  2. seachBar.backgroundColor=[UIColor clearColor];
  3. for (UIView *subview in seachBar.subviews)
  4. {
  5. if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
  6. {
  7. [subview removeFromSuperview];
  8. break;
  9. }
  10. }

第二种解决的方法:

  1. [[searchbar.subviews objectAtIndex:0]removeFromSuperview];

  2、

    UISearchBar* m_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 44, 320, 41)];

    m_searchBar.delegate = self;

    m_searchBar.barStyle = UIBarStyleBlackTranslucent;

    m_searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

    m_searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;

    m_searchBar.placeholder = _(@"Search");

    m_searchBar.keyboardType =  UIKeyboardTypeDefault;

    //为UISearchBar添加背景图片

    UIView *segment = [m_searchBar.subviews objectAtIndex:0];

    UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Images/search_bar_bg.png"]];        [segment addSubview: bgImage];

    //<---背景图片

    [self.view addSubview:m_searchBar];

    [m_searchBar release];

3:取消UISearchBar调用的键盘

  1. [searchBar resignFirstResponder];

添加UISearchBar的两种方法:

代码

  1. UISearchBar *mySearchBar = [[UISearchBar alloc]
  2. initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)];
  3. mySearchBar.delegate = self;
  4. mySearchBar.showsCancelButton = NO;
  5. mySearchBar.barStyle=UIBarStyleDefault;
  6. mySearchBar.placeholder=@"Enter Name or Categary";  //输入框中原始的文字
  7. mySearchBar.keyboardType=UIKeyboardTypeNamePhonePad;
  8. [self.view addSubview:mySearchBar];
  9. [mySearchBar release];

3.在 tableview上添加:

代码

       //add Table

UITableView *myBeaconsTableView = [[UITableView alloc]   initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-40)      style:UITableViewStylePlain];

myBeaconsTableView.backgroundColor = [UIColor whiteColor];

myBeaconsTableView.delegate=self;

myBeaconsTableView.dataSource=self;

[myBeaconsTableView setRowHeight:40];

// Add searchbar

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40)];

searchBar.placeholder=@"Enter Name";

searchBar.delegate = self;

myBeaconsTableView.tableHeaderView = searchBar;

searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;

[searchBar release];

[self.view addSubview:myBeaconsTableView];

[myBeaconsTableView release];

4.删除搜索框背景

[[mSearchBar.subviewsobjectAtIndex:0]removeFromSuperview];

// 删除searchBar输入框的背景

for (UIView* subview  in mSearchBar.subviews) {

if ([subview isKindOfClass:[UITextField class]]) {

UITextField * searchField = (UITextField*)subview;

searchField.leftView=nil;//隐藏搜索小图标

[searchField setBackground:nil];

[searchField setBorderStyle:UITextBorderStyleNone];

break;

}

}

转载于:https://www.cnblogs.com/Cristen/archive/2012/11/20/2779155.html

UISeatchBar相关推荐

最新文章

  1. python str byte编码_Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题...
  2. 【河南省多校脸萌第六场 E】LLM找对象
  3. RandomAccessFile类解析
  4. 遇到网络问题你是怎么解决的?
  5. 基于Web的浮动图片墙
  6. Spark学习笔记:索引分区映射
  7. python 随机数_python项目实战:实现蒙特卡罗方法,求物体阴影面积
  8. php5apache2.dll,Apache2.2.8 + PHP5.2.5不能加载php5apache2.dll
  9. 潜在语义分析(TF-IDF、LSA)
  10. 基于SSM开发智夫子在线考试系统
  11. 互联网中几种常用的传输协议
  12. 利用VB操作目录和文件夹
  13. 业余10—解决只能打开网页不能打开QQ或视频软件问题
  14. 终端模拟器编译c语言,开源一个Flutter编写的完整终端模拟器
  15. Python 去除单色背景
  16. 40亿美金史上最大 ICO,EOS 疑云
  17. 给联想拯救者15isk装win7遇到的问题
  18. 第1090期AI100_机器学习日报(2017-09-12)
  19. PMI-PMP®远程模考三 正题数
  20. oracle事务管理器,全能数据库盘问分析器中的事务管理在oracle中的应用.doc

热门文章

  1. android stub.asinterface是什么意思
  2. 互联网研发中负载均衡算法一点探索
  3. EasyDarwin开源音频解码项目EasyAudioDecoder:EasyPlayer Android音频解码库(第二部分,封装解码器接口)...
  4. 使用FastCoder写缓存单例
  5. MS的.net源码地址
  6. 【浸入式英文学习方式】山姆莱萌帮助孩子建立扎实语言功底
  7. 08-GCD常用方法
  8. 黄聪:如何用代码设置控制自己网站的网页在360浏览器打开时强制优先使用极速模式,而非兼容模式...
  9. 远程管理MAC OS
  10. laravel CURD ORM