Q: 如果想要做到上图所示的搜索框需要怎么设置?
搜索框的样式设置都是基于 UISearchController 的 searchBar 也就是 UISearchBar 设置的。
但是 UISearchBar 并没有提供多少自定义设置。 图标相关的设置也主要是系统提供的几种简单样式。
而iOS11和iOS11以下的搜索框由于样式不一样,很多属性的设置不尽相同。
所以这篇文章就是对怎么修改 UISearchController 样式的梳理
首先 UISearchController 主要是对 UISearchBar 的封装,而 UISearchBar 又包含了 UITextField 和 UINavigationButton (取消按钮)
有两种方法可以获取,KVC或者遍历子视图。
我们这里采用KVC,效率比较高。
这里我们讨论三种图标的设置
如果设置了bookmark,在没有输入的时候显示bookmark,输入的时候显示清除按钮
这三种图标都可以通过以下方法来设置。
除此之外也可以获取具体的UI组件来设置
如果是想用默认的图片但只是想改变其颜色的话,
以修改放大镜颜色为例。
这里我们定义一个属性,然后通过属性监测器来应用到图片上。这里我们取的是在文章开头定义的 searchField 。然后取它的左视图 UIImageView 来修改。
可以通过获取原图片然后修改图片的。
其它图标的修改请参考文末附的源码。
这部分的设置都是跟 UISearchField 的一样
通过设置 searchField 的 tintColor 即可修改光标颜色
修改 searchBar 的 tintColor 也可以达到效果,因为子视图的 tintColor 会自动继承父级视图。
但是设置 searchBar 的 tintColor 还会修改取消按钮的颜色。如果你想要他们同一个颜色话,直接设置 seachBar 更好。
iOS11以下的直接设置 searchField?.backgroundColor 即可。
而iOS11的要设置
取消按钮就是在输入的时候显示的cancel按钮。只要设置 showsCancelButton 即可。
而如果要自定义这个按钮的话,我们采用的也是遍历子视图获取到这个按钮的方法。
然后在 searchBar 的代理事件的 searchBarShouldBeginEditing 或者 searchBarDidBeginEditing 中调用即可。
其它还有一些细节的设置可以参考下面的源码
源码 已经提交到Github上了。
并且简化了所有的属性和代理方法。
欢迎大家来提交问题和分支。
如果觉得有帮助的话记得给颗星:P
原文: https://www.jianshu.com/p/26f951f2a9b5UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 但是没有直接 *** 作背景的属性。方法一:是直接将 UISearchBarBackGround移去(这个方法ios13中不可用会闪退)。方法二:是创建一个UIView设置其颜色加载到UISearchBarBaceGround上作为UISearchBar的背景颜色
UISearchBar *seachBar=[[UISearchBar alloc] init]
//修改搜索框背景
seachBar.backgroundColor=[UIColor clearColor]
//去掉搜索框背景
//1.
[[searchbar.subviews objectAtIndex:0]removeFromSuperview]
//2.
for (UIView *subview in seachBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview]
break
}
}
//3自定义背景
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"40-di.png"]]
[mySearchBar insertSubview:imageView atIndex:1]
//4输入搜索文字时隐藏搜索按钮,清空时显示
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
searchBar.showsScopeBar = YES
[searchBar sizeToFit]
[searchBar setShowsCancelButton:YES animated:YES]
return YES
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
searchBar.showsScopeBar = NO
[searchBar sizeToFit]
[searchBar setShowsCancelButton:NO animated:YES]
return YES
}
//改变搜索按钮文字
//改变UISearchBar取消按钮字体
for(id cc in [searchBar subviews])
{
if([cc isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)cc
[btn setTitle:@"搜索" forState:UIControlStateNormal]
}
方法二:
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
//--->背景图片
UIView *segment = [m_searchBar.subviews objectAtIndex:0]
UIImageView *bgImage = [[UIImageView alloc] initWithImage: [UIImageimageNamed:@"Images/search_bar_bg.png"]]
[segment addSubview: bgImage]
//<---背景图片
[self.view addSubview:m_searchBar]
[m_searchBar release]
其实就是在 UISearchBarDelegate 的- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
先设置取消按钮可以显示,才能去设置它的一些属性
[searchBar setShowsCancelButton:YES animated:YES]
然后呢 通过找 SearchBar 子 view 的形式找到一个UINavigationButton 的私有类,这个类苹果没有公开,但是它是个 UIButton 的子类,通过强制转换后你可以设置它的一些属性
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)