遇到这个问题,虽然解决了,但接下来还是得好好找找问题的原因所在~~ 问题重现 未修改前的代码 self.tableView.tableHeaderView = self.searchController.searchBar ; 直接将searchBar设置为tableView的tableHeaderView,然后奇葩的现象就出现了,手动下拉刷新看不到刷新控件,待放手后才会出现,具体看图 ? :CGRectMake(0, 0, KSCREEN_WIDTH, 45)]; [view addSubview:self.searchController.searchBar]; // 添加 searchbar 到 headerview self.tableView.tableHeaderView = view 方案二 -(UIView *)tableView:(UITableView *) tableView viewForHeaderInSection:(NSInteger)section { return self.searchController.searchBar; }
= UITextAutocapitalizationTypeNone; m_searchBar.placeholder= _(@"Search"); m_searchBar.keyboardType ]; [m_searchBar release]; 1.2.2 取消UISearchBar调用的键盘 [searchBar resignFirstResponder]; 1.2.3 添加UISearchBar = @"Enter Name"; searchBar.delegate = self; myBeaconsTableView.tableHeaderView = searchBar ; self.searchBar.keyboardType = UIKeyboardTypeNamePhonePad; self.searchBar.showsBookmarkButton = YES : @"搜索城市中文/拼音/字母"]; self.searchBar.delegate = self; [self.view addSubview: _searchBar
- 10)) searchBar?.barTintColor = UIColor.clear if let textfiled = searchBar?. tintColor = UIColor.white searchBar?.delegate = self searchBar?. placeholder = placeHolder searchBar?. title = title searchBar.showsCancelButton = false searchBar.isHidden = true vc ) { searchStringCallBack(searchBar.text ??
iOS中UISearchBar(搜索框)使用总结 初始化:UISearchBar继承于UIView,我们可以像创建View那样创建searchBar UISearchBar * bar = [ [self.view addSubview:bar]; @property(nonatomic) UIBarStyle barStyle; 这个属性可以设置searchBar void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; 已经结束编辑的回调 - (void)searchBar :(UISearchBar *)searchBar textDidChange:(NSString *)searchText; 编辑文字改变的回调 - (BOOL)searchBar:(UISearchBar *)searchBar; 搜索结果按钮点击的回调 - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange
NO; //self.searchController.hidesNavigationBarDuringPresentation = NO; self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x,self.searchController.searchBar.frame.origin.y ,self.searchController.searchBar.frame.size.width,44); self.tableView.tableHeaderView = self.searchController.searchBar ; self.searchController.searchBar.delegate = self; } 3 实现UISearchController的UISearchResultsUpdating SearchResultVC *)navController.topViewController; [self filterContentForSearchText:self.searchController.searchBar.text
(Scale_X(20), Scale_Y(10), self.bounds.size.width-Scale_X(40), Scale_Y(36))]; searchBar.placeholder = @"请输入搜索关键字"; searchBar.showsCancelButton = YES; searchBar.delegate =self; searchBar.layer.cornerRadius=4; searchBar.clipsToBounds = YES; //不设置的话没有圆角 searchBar.delegate = self; [searchBar setBackgroundImage:[UIImage imageNamed:@"sousuoBg"]]; [searchBar ]; #pragma mark - UISearchBarDelegate -(void)searchBar:(UISearchBar *)searchBar textDidChange
; //类型 //searchBar.barStyle = UIBarStyleBlack; //占位符 searchBar.placeholder = @"请输入搜索内容"; //副标题 //searchBar.prompt ; //显示按钮 searchBar.showsBookmarkButton = YES; searchBar.showsCancelButton = YES; searchBar.showsSearchResultsButton = YES; searchBar.showsScopeBar = YES; [searchBar setScopeButtonTitles:[NSArray arrayWithObjects:@"a" , @"b", @"c", @"d",nil]]; //设置代理 searchBar.delegate = self; - (void)searchBar:(UISearchBar *)searchBar 如果不为空,代表我们在搜索,tableView要显示搜索结果 if (searchBar.text == nil || [searchBar.text isEqualToString:@""]) {
searchController // 将搜索框视图�设置为tableView的tableHeaderView tableView.tableHeaderView = searchController.searchBar 添加searchBar 设置搜索框 // 搜索框 let bar = searchController.searchBar // 样式 bar.barStyle = .default // 设置光标及取消按钮的颜色 主要代码 上面仅作演示,下面的代码为searchBar的主要设置 let commonBgColor = RGBA(r: 0.94, g: 0.94, b: 0.96, a: 1.00) searchBar.barTintColor = commonBgColor // 搜索框 searchBar.barStyle = .default searchBar.tintColor = RGBA(r: 0.12, g: 0.74, b : .default) // 右侧语音 searchBar.showsBookmarkButton = true searchBar.setImage(#imageLiteral(resourceName
=NO; _searchController.searchBar.barTintColor=[UIColor brownColor]; //设置searchbar的边框颜色和背景颜色一致 _searchController.searchBar.layer.borderWidth=1; _searchController.searchBar.layer.borderColor = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y , self.searchController.searchBar.frame.size.width, 44); _searchController.searchBar.delegate=self 怎么变中文呢,如下 -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { [searchBar setShowsCancelButton
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:barView]; 2.如何找到searchBar ,8.0); searchBar.delegate = self; searchBar.barTintColor = [UIColor clearColor]; UITextField ; searchBar.barStyle = UIBarStyleDefault; [searchBar.layer setBorderWidth:1.0]; searchBar.layer.borderColor = [[UIColor clearColor] CGColor]; searchBar.backgroundColor=[UIColor clearColor]; searchBar.placeholder = @"请输入关键字名称"; [searchView addSubview:searchBar]; [searchBar becomeFirstResponder]; self.navigationItem.leftBarButtonItem
UISearchController = { let controller = UISearchController(searchResultsController: nil) controller.searchBar.searchBarStyle = .minimal controller.searchBar.placeholder = "搜索学校" return controller }() UISearchController = { let controller = UISearchController(searchResultsController: nil) controller.searchBar.searchBarStyle = .minimal controller.searchBar.placeholder = "搜索学校" return controller }() refreshBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: nil) // 将searchBar
selector(segmentedControlClick:) forControlEvents:UIControlEventValueChanged]; self.searchController.searchBar.x = 0; self.searchController.searchBar.y = 0; [headerView addSubview:self.searchController.searchBar]; = self; _searchController.dimsBackgroundDuringPresentation = NO; _searchController.searchBar.placeholder = @"搜索"; [_searchController.searchBar sizeToFit]; } return _searchController; } 运行后发现搜索栏的位置偏移了 = @"搜索"; [_searchController.searchBar sizeToFit]; } return _searchController; } 到此搜索框就能正常是显示了
FilterableProductTable (orange): 包含所有的子组件,是个容器 SearchBar (blue): 用于用户输入交互 ProductTable (green): 呈现数据项并根据用户输入过滤数据 至此,我们将这五个组件的继承关系确定下来: FilterableProductTable SearchBar ProductTable ProductCategoryRow ProductRow step2 在每一个状态期, 确保每个组件都会根据当前状态来渲染 寻找其共同的祖先组件 在继承链中层级较高的组件拥有state 回到我们的应用中, ProductTable需要根据state来过滤数据,SearchBar 我们需要给该组件设置getInitialState方法设置组件的初始状态,并且通过props将状态传递给ProductTable和SearchBar,最后我们就可以在ProductTable和SearchBar 所以我们通过在ProductTable和SearchBar设置事件监听函数,并且每当函数触发时setState当前的状态,促使组件渲染重绘,完成数据的动态呈现。
实现搜索 我们是把searchBar抽出来放到了一个单独的类,我们可以把数据传进去,根据搜索的内容匹配搜索的结果在在searchPage展示,也可以把searcheBar的值告诉当前页面,之后进行搜索结果展示 2.1 searchBar搜索 class SearchBar extends StatefulWidget { final List<ChatModel>? searchResult; SearchBar({this.listData,this.searchResult}); @override _SearchBarState createState onChanged; SearchBar({this.onChanged}); (滑动显示更多) 传递出去 if(widget.onChanged ! (text); } (滑动显示更多) 在page页接收 SearchBar(listData: widget.listData, searchResult:(List<ChatModel> list
target: self, selector: #selector(findCancel)) }() 提供查找并设置取消按钮的方法 func findCancel() { let btn = searchBar.value normal) cancel.setTitleColor(UIColor.orange, for: .highlighted) } } 代理方法 设置代理为当前控制器,并实现代理方法 searchBar.delegate = self func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { if !
像素宽的黑色边框,去不掉 把 UISearchBar 放在 UITableView 头部的代码在 iOS 8 不起作用 tableView.tableHeaderView = searchController.searchBar // now working 解决问题 1 searchController.searchBar.layer.borderWidth = 1 searchController.searchBar.layer.borderColor = // your background color 解决问题 2 我发现 tableHeaderView 是可以被其他 view 有效填充的,不知道为什么 searchBar 就是放不进去,所以直接找个大小一样的 UIView 作为容器放在 searchBar 下面就好了。 64, view.frame.width, 44)) containerView.backgroundColor = // your background color searchController.searchBar.frame
我们可以通过循环遍历出UISearchBar上所有展示出来的子视图 for(UIView*viewin[[[_searchController.searchBar subviews]lastObject valueForKey:@"_background"]; UITextField*searchField = [_searchController.searchBar valueForKey:@"_ searchField"]; UIButton*cancelButton = [_searchController.searchBar valueForKey:@"_cancelButton"]; 当我们获取 cancelButton时,一定要确保cancelButton包含在了UISearchBar中,必要时可以提前调用: [_searchController.searchBar setShowsCancelButton :YES animated:NO]; 去掉搜索框背景 for(UIView*viewin[[[_searchController.searchBar subviews]lastObject]subviews
数值 设置当前选中的标签 bindchange 函数 绑定选中的标签发生变化的回调事件 3.Searchbar 组件 Searchbar 组件,引用路径为: "weui-miniprogram/searchbar/searchbar" 示例代码: <mp-searchbar placeholder="查找朋友" ></mp-searchbar> 下面展示了 Searchbar 组件的示例效果。
= .minimal controller.searchBar.sizeToFit() controller.searchResultsUpdater = self controller.searchBar.delegate = self return controller }() let schoolArray = ["清华大学", updateSearchResults(for searchController: UISearchController) { if let text = searchController.searchBar.text extension ViewController: UISearchBarDelegate { // MARK: 点击搜索 func searchBarSearchButtonClicked(_ searchBar : UISearchBar) { if let text = searchBar.text { searchArray = schoolArray.filter
FilterableProductTable(橙色):整个示例程序 SearchBar(蓝色):接收所有的用户输入 ProductTable(绿色):根据用户输入显示和过滤数据 ProductCategoryRow 原型中一个部件在另一个部件中,层次结构中应该为父子层级关系: FilterableProductTable SearchBar ProductTable ProductCategoryRow ProductRow 接着,将filterText和inStockOnly作为属性传递到ProductTable和SearchBar中。 最后,用这些属性过滤ProductTable的数据,同时显示在SearchBar表单中。 你会开始看到应用如何反应:设置filterText为“ball”然后刷新应用。你会看到数据表正确地刷新了。 由于组件应该只更新自己的状态,FilterableProductTable会将回调传递给SearchBar,只要状态更新就会触发。我们可以使用输入上的onChange事件来通知它。