zoukankan      html  css  js  c++  java
  • UISearchBar相关

    前段时间做了一个UISearchBar相关的需求,今天也总结以下。

    1、UISearchBar自定义背景、取消按钮中文设置

    1. UISearchBar *seachBar=[[UISearchBar alloc] init];  
    2.   
    3. //修改搜索框背景  
    4. seachBar.backgroundColor=[UIColor clearColor];  
    5.   
    6. //去掉搜索框背景  
    7. [[searchbar.subviews objectAtIndex:0]removeFromSuperview];  
    8.   
    9. for (UIView *subview in seachBar.subviews)   
    10. {    
    11. if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])  
    12. {    
    13. [subview removeFromSuperview];    
    14. break;    
    15. }    
    16. }   
    17.   
    18. //自定义背景  
    19. UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"40-di.png"]];  
    20. [searchBar insertSubview:imageView atIndex:1];  
    21.   
    22. //输入搜索文字时隐藏搜索按钮,清空时显示  
    23. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {    
    24. searchBar.showsScopeBar = YES;    
    25. [searchBar sizeToFit];    
    26. [searchBar setShowsCancelButton:YES animated:YES];    
    27. return YES;    
    28. }    
    29.   
    30. - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {    
    31. searchBar.showsScopeBar = NO;    
    32. [searchBar sizeToFit];    
    33. [searchBar setShowsCancelButton:NO animated:YES];    
    34. return YES;    
    35. }    
    36.   
    37. //修改UISearchBar取消按钮中文字体  
    38.     for (id aa in [searchBar subviews]) {  
    39.         if ([aa isKindOfClass:[UIButton class]]) {  
    40.             UIButton *btn = (UIButton *)aa;  
    41.             [btn setTitle:@"取消" forState:UIControlStateNormal];  
    42.         }  
    43.     }  

    2、UISearchBar 的delegate方法

    1. #pragma mark 搜索控件  
    2. //键盘搜索按钮被点击时触发
    3. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{  
    4.     UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];  
    5.     FirstViewController *fVC = [mainStory instantiateViewControllerWithIdentifier:@"goFirstView"];  
    6.     fVC.showStr = self.searchBar.text;  
    7.     [self presentModalViewController:fVC animated:YES];  
    8. }  
    9.   
    10. //搜索框输入内容改变时触发  
    11. - (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{  
    12.       
    13. }  
    14.   
    15. //焦点进入搜索框时触发  
    16. - (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar{  
    17.     self.soundBtn.hidden = YES;  
    18.     [self searchBar:searchBar activate:YES];  
    19.       
    20. }  
    21.   
    22. //取消搜索按钮点击时触发  
    23. - (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar {  
    24.     self.searchBar.text = @"";  
    25.     self.soundBtn.hidden = NO;  
    26.     [self searchBar:searchBar activate:NO];  
    27. }  
    28.   
    29. - (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active{  
    30.     if (!active) {    
    31.         [self.searchBar resignFirstResponder];    
    32.     }   
    33.       
    34.     [self.searchBar setShowsCancelButton:active animated:YES];  
    35.     //修改UISearchBar取消按钮字体  
    36.     for (id aa in [searchBar subviews]) {  
    37.         if ([aa isKindOfClass:[UIButton class]]) {  
    38.             UIButton *btn = (UIButton *)aa;  
    39.             [btn setTitle:@"取消" forState:UIControlStateNormal];  
    40.         }  
    41.     }  
    42. }  
  • 相关阅读:
    性能测试总结(一)测试流程
    WSDL入门
    Python中的while循环和for循环
    python中的变量
    吴恩达《机器学习》章节2单变量线性回归
    吴恩达《机器学习》章节1绪论:初识机器学习
    新式类多继承的查找顺序
    python2x和python3x的一些区别
    类方法和静态方法
    @property的使用
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/3885609.html
Copyright © 2011-2022 走看看