设置之前:

设置之后:

代码如下:

//
// ViewController.m
// UISearchBarDemo
//
// Created by 思 彭 on 17/3/24.
// Copyright © 2017年 思 彭. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UISearchBarDelegate> @property (nonatomic, strong) UISearchBar *searchBar;/**<搜索框 */ @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.title = @"UISearchBar";
[self setupSearchBar];
[self setSearchBar];
} //添加搜索框
- (void)setupSearchBar { self.searchBar = [[UISearchBar alloc]init];
self.searchBar.frame = CGRectMake(, , self.view.frame.size.width, );
self.searchBar.delegate = self;
// self.searchBar.searchBarStyle =UISearchBarStyleMinimal;
self.searchBar.barTintColor = [UIColor colorWithRed:238.0/ green:238.0/ blue:238.0/ alpha:1.0]; // 去除了分割线,需要设置背景颜色
self.searchBar.backgroundColor = [UIColor colorWithRed:238.0/ green:238.0/ blue:238.0/ alpha:1.0];
[self.searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
self.searchBar.placeholder = @"搜索";
[self.searchBar sizeToFit];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
[self.navigationController.navigationBar addGestureRecognizer:tap];
[self.view addSubview:self.searchBar];
} // 去掉SearchBar上面两条线
- (void)setSearchBar { for (UIView *obj in [self.searchBar subviews]) {
for (UIView *objs in [obj subviews]) {
if ([objs isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){
[objs removeFromSuperview];
}
}
if ([obj isKindOfClass:NSClassFromString(@"UISearchBarBackground")]){
[obj removeFromSuperview];
}
}
} #pragma marl - UISearchBarDelegate - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { self.searchBar.showsCancelButton = YES;
return YES;
} - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { self.searchBar.showsCancelButton = NO;
searchBar.text = @"";
[self.searchBar resignFirstResponder];
} - (void)tapClick:(UITapGestureRecognizer *)tap { self.searchBar.showsCancelButton = NO;
[self.searchBar resignFirstResponder];
} //点击搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { self.searchBar.showsCancelButton = NO;
[searchBar resignFirstResponder];
} @end

最新文章

  1. 在你的ASP.NET MVC中使用查找功能
  2. sed教程
  3. 全国高校网安联赛Web专场~WriteUp
  4. (转)LSTM NEURAL NETWORK FOR TIME SERIES PREDICTION
  5. Node.js与Sails~Model和ORM的持久化
  6. nginx作为负载均衡服务器——测试
  7. 几张图弄明白ios布局中的尺寸问题
  8. vector族函数
  9. javascript 中的 call
  10. The version of CocoaPods used to generate the lockfile (*) is higher than the version of the current executable (*). Incompatibility issues may arise.
  11. C# .NET3.5 改为 到.NET2.0 时 TypedTableBase 报错解决方法
  12. CSS3实现图片鼠标悬浮放大效果
  13. STM32F4系列单片机上使用CUBE配置MBEDTLS实现pem格式公钥导入
  14. 我的DIY作品
  15. 腾讯云主机安装登录mysql失败--解决方案[重置root密码并实现远程连接]
  16. 嵌入式开发之uart---编程
  17. 如何在忘记mysql的登录密码时更改mysql登录的密码(window及linux)
  18. Linux命令行上执行操作,不退回命令行的解决方法
  19. hdoj2111 Saving HDU
  20. 实现qsort(和qsort差一个数量级啊,伤自尊了)

热门文章

  1. The Tower HDU - 6559 (解析几何)
  2. CF #546 D.E
  3. hbase单机搭建
  4. python 示例代码1
  5. 使用IDEA 搭建一个 SpringBoot + Hibernate + Gradle 项目
  6. Python文件查找
  7. Codeforces 884E E. Binary Matrix
  8. php回顾(3)系统函数
  9. SQL中where in的用法
  10. Java线程之ThreadLocal