转载请标明出处:http://blog.csdn.net/android_ls/article/details/39993433
測试的手机IOS系统版本为:6.1.3,实现过程例如以下:
1、加入UISearchBar到父View
_searchBar = [[UISearchBar alloc]init]; _searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, kSeachBarH); _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; _searchBar.delegate = self; _searchBar.placeholder = @"请输入姓名、公司名称、公司产品名称"; [self.view addSubview:_searchBar];
2、改动搜索框背景
UIImage *img = [UIImage resizedImage:@"find_bg.png"]; [_searchBar setBackgroundImage:img];
3、改动搜索输入框内左側的指示图标
[_searchBar setImage:[UIImage resizedImage:@"ic_search.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
4、改动搜索输入文本的背景
[_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"login_btn_input_side.png"] forState:UIControlStateNormal];
注:对于设计人员提供的搜索输入文本的背景。若提供的是一个圆角的小方块,按常理我们会使用拉伸图片的中间部分的方法,经測试显示效果例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW5kcm9pZF9scw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="320" height="480" >
若让设计人员又一次提供一张固定高度的图片(比方高是60),当做搜索输入文本的背景。效果图例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW5kcm9pZF9scw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" width="320" height="480" >
5、改动UISearchBar右側的取消button文字颜色及背景图片
#pragma mark 搜索框的代理方法,搜索输入框获得焦点(聚焦) -(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { [searchBar setShowsCancelButton:YES animated:YES]; // 改动UISearchBar右側的取消button文字颜色及背景图片 for (UIView *searchbuttons in [searchBar subviews]){ if ([searchbuttons isKindOfClass:[UIButton class]]) { UIButton *cancelButton = (UIButton*)searchbuttons; // 改动文字颜色 [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; // 改动button背景 [cancelButton setBackgroundImage:[UIImage resizedImage:@"login_btn_login.png"] forState:UIControlStateNormal]; [cancelButton setBackgroundImage:nil forState:UIControlStateHighlighted]; } } }
注:改动取消button文字颜色及背景图片的代码片段,一定要放到取消button会显示代理方法中改动,否则遍历找不着呀,那就改动不了了。
版权声明:本文博主原创文章,博客,未经同意不得转载。