三站合一的网站怎么做,食品营销型网站,郴州网站建设公司在哪里,公司取名三个字推荐搜索#xff0c;无疑可以使用UISearchBar控件#xff01; 那就先了解一下UISearchBar控件吧#xff01; UISearchBar控件就是要为你完成搜索功能的一个专用控件。它集成了很多你意想不到的功能和特点#xff01; 首先#xff0c;还是来普及一下UISearchBar控件API相关的属…搜索无疑可以使用UISearchBar控件 那就先了解一下UISearchBar控件吧 UISearchBar控件就是要为你完成搜索功能的一个专用控件。它集成了很多你意想不到的功能和特点 首先还是来普及一下UISearchBar控件API相关的属性和方法吧 UISearchBar属性相关 _searchBar [[UISearchBar alloc] initWithFrame:CGRectZero];// 初始化不解释 [self.searchBar setPlaceholder:Search];// 搜索框的占位符 [self.searchBar setPrompt:Prompt];// 顶部提示文本,相当于控件的Title [self.searchBar setBarStyle:UIBarMetricsDefault];// 搜索框样式 [self.searchBar setTintColor:[UIColor blackColor]];// 搜索框的颜色当设置此属性时barStyle将失效 [self.searchBar setTranslucent:YES];// 设置是否透明 [self.searchBar setBackgroundImage:[UIImage imageNamed:image0]];// 设置背景图片 [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:image3]forState:UIControlStateNormal];// 设置搜索框中文本框的背景 [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:image0]forState:UIControlStateHighlighted]; [self.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(30,30)];// 设置搜索框中文本框的背景的偏移量 [self.searchBar setSearchResultsButtonSelected:NO];// 设置搜索结果按钮是否选中 [self.searchBar setShowsSearchResultsButton:YES];// 是否显示搜索结果按钮 [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(30, 0)];// 设置搜索框中文本框的文本偏移量 [self.searchBar setInputAccessoryView:_btnHide];// 提供一个遮盖视图 [self.searchBar setKeyboardType:UIKeyboardTypeEmailAddress];// 设置键盘样式 // 设置搜索框下边的分栏条 [self.searchBar setShowsScopeBar:YES];// 是否显示分栏条 [self.searchBar setScopeButtonTitles:[NSArrayarrayWithObjects:Singer,Song,Album, nil]];// 分栏条栏目 [self.searchBar setScopeBarBackgroundImage:[UIImage imageNamed:image3]];// 分栏条的背景颜色 [self.searchBar setSelectedScopeButtonIndex:1];// 分栏条默认选中的按钮的下标 [self.searchBar setShowsBookmarkButton:YES];// 是否显示右侧的“书图标” [self.searchBar setShowsCancelButton:YES];// 是否显示取消按钮 [self.searchBar setShowsCancelButton:YES animated:YES]; // 是否提供自动修正功能这个方法一般都不用的 [self.searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];// 设置自动检查的类型 [self.searchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];// 是否提供自动修正功能一般设置为UITextAutocorrectionTypeDefault self.searchBar.delegate self;// 设置代理 [self.searchBar sizeToFit]; myTableView.contentInset UIEdgeInsetsMake(CGRectGetHeight(self.searchBar.bounds), 0, 0, 0); [self.view addSubview:myTableView]; [myTableView addSubview:self.searchBar]; 这么多属性其实看起来多你实际去操作事件一下就发现很简单的 绝大多部分都是定义一些外观的东西了解了各个属性一定能满足你设计出你想要的外观效果 然后解释一下我个人觉的比较有趣和重要的属性 1.property (nonatomic, readwrite, retain) UIView *inputAccessoryView;属性 例如 [self.searchBar setInputAccessoryView:your_View];// 提供一个遮盖视图 当处于UISearchBar焦点状态下输入框正要输入内容时会有一个遮盖视图。 你翻看一下iPhone手机上的电话本搜索功能。那个遮盖视图就是一个半透明的黑色View。 查看了一下API是iOS 6.0 以及以后新加入的 那么就意味这 iOS 6.0 之前的系统是不兼容的。那么怎么才能达到这个类似的效果呢 变通一下其实很简单仍然设置一个按钮初始状态下该UIButton控件透明度设置为0并且在控件取得焦点时设置透明度为1。 小技巧如果要设置这个属性那么就最好定义一个UIButton控件这样当点击该遮盖层的话可以利用按钮事件 设置[self.searchBar resignFirstResponder];让搜索框放弃第一焦点。iPhone电话薄也是这么做的感觉很人性化。 迷惑还有一个小的问题当我让UISearchBar显示取消按钮时当我让UISearchBar失去焦点时我的取消按钮也不能点击了。衰啊。 看了一下iPhone电话薄的UISearchBar竟然可以也找了很久都不知道是怎么回事大概苹果又开始玩私有API了吧。 解决方法很暴力但是很好用在UISearchBar上原来取消按钮的位置上覆盖一个UIButton设置成一样的。呵呵。可以了。 类似如下 // 遮盖层 _btnAccessoryView[[UIButton alloc] initWithFrame:CGRectMake(0, 44, BOUNDS_WIDTH,BOUNDS_HEIGHT)]; [_btnAccessoryView setBackgroundColor:[UIColor blackColor]]; [_btnAccessoryView setAlpha:0.0f]; [_btnAccessoryView addTarget:self action:selector(ClickControlAction:)forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_btnAccessoryView]; // 遮罩层按钮-点击处理事件 - (void) ClickControlAction:(id)sender{ NSLog(handleTaps); [self controlAccessoryView:0]; } // 控制遮罩层的透明度 - (void)controlAccessoryView:(float)alphaValue{ [UIView animateWithDuration:0.2 animations:^{ //动画代码 [self.btnAccessoryView setAlpha:alphaValue]; }completion:^(BOOL finished){ if (alphaValue0) { [self.searchBar resignFirstResponder]; [self.searchBar setShowsCancelButton:NO animated:YES]; [self.navigationController setNavigationBarHidden:NO animated:YES]; } }]; } 2.property(nonatomic,assign) id/bUISearchBarDelegate delegate;属性 例如 self.searchBar.delegate self; 说到这个属性就是设置委托了。 UISearchBarDelegate委托定义了很多关于搜索框的一些操作数据的协议方法 先来个特写把x协议的家庭成员列出来 protocol UISearchBarDelegate optional - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar; - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar; - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar; - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar; - (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar; - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar; - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope; end 这不需要解释吧看方法名称就能了解 我们来看一看常用的委托方法吧。 #pragma mark - UISearchBarDelegate 协议 // UISearchBar得到焦点并开始编辑时执行该方法 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ [self.searchBar setShowsCancelButton:YES animated:YES]; [self.navigationController setNavigationBarHidden:YES animated:YES]; [self controlAccessoryView:0.9];// 显示遮盖层。 return YES; } // 取消按钮被按下时执行的方法 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ [self.searchBar resignFirstResponder]; [self.searchBar setShowsCancelButton:NO animated:YES]; [liveViewAreaTable searchDataBySearchString:nil];// 搜索tableView数据 [self.navigationController setNavigationBarHidden:NO animated:YES]; [self controlAccessoryView:0];// 隐藏遮盖层。 } // 键盘中搜索按钮被按下执行的方法 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ NSLog(---%,searchBar.text); [self.searchBar resignFirstResponder];// 放弃第一响应者 [liveViewAreaTable searchDataBySearchString:searchBar.text]; [self.navigationController setNavigationBarHidden:NO animated:YES]; [self controlAccessoryView:0];// 隐藏遮盖层。 } // 当搜索内容变化时执行该方法。很有用可以实现时实搜索 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{ NSLog(textDidChange---%,searchBar.text); [liveViewAreaTable searchDataBySearchString:searchBar.text];// 搜索tableView数据 [self controlAccessoryView:0];// 隐藏遮盖层。 } 3.遍历UISearchBar控件的子控件这样可以针对不同的子视图来设置外观了。 for(id subView in [self.searchBar subviews]){ if([subView isKindOfClass:[UIButton class]]){ UIButton *btn (UIButton *)subView; [btn setTitle:取消 forState:UIControlStateNormal]; } } 转载于:https://www.cnblogs.com/IosLearnNote/p/4432996.html