• 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. }  
  • 相关阅读:
    Kali渗透测试工具-netcat
    信息收集工具-dimtry
    Beef xss神器
    Scapy编写ICMP扫描脚本
    全国职业技能大赛信息安全管理与评估-MySQL弱口令利用
    crawler 听课笔记 碎碎念 2 一些爬虫须知的基本常识和流程
    crawler 听课笔记 碎碎念 3 关于python的细枝末节的回顾复习
    关于互信息(Mutual Information),我有些话要说
    最让人头疼的清洗数据过程----选择合适的方式快速命中所需的数据
    利用小虫虫做一枚合格宅男,果然牡丹花下做鬼也风流
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/3885609.html
Copyright © 2020-2023  润新知