美文网首页
iOS UISearchBar轻松改变搜索图标和占位符坐标

iOS UISearchBar轻松改变搜索图标和占位符坐标

作者: 顺其自然_Cao | 来源:发表于2018-02-09 16:55 被阅读0次
     //搜索框
        UISearchBar *searchBar = [[UISearchBar alloc] init];
    //    searchBar.delegate = self;
        searchBar.frame = CGRectMake(10, 0, screenW-80, titleView.bounds.size.height);
        searchBar.layer.cornerRadius = 17;
        searchBar.layer.masksToBounds = YES;
        [searchBar.layer setBorderWidth:8];
        [searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];  //设置边框为白色
        searchBar.placeholder = @"请输入目的地";
        searchBar.translucent = YES;
        searchBar.searchBarStyle = UISearchBarStyleProminent;
        //改变占位符的字体大小颜色
        UITextField * searchField = [searchBar valueForKey:@"_searchField"];
        [searchField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [searchField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        //改变左边搜索图标坐标位置。后面的占位符苹果已经做好约束总是跟随在搜索🔍图标后面。
        [searchBar setPositionAdjustment:UIOffsetMake(60, 0) forSearchBarIcon:UISearchBarIconSearch];
    设置搜索栏中图片的位置偏移,图片的枚举如下:
    typedef NS_ENUM(NSInteger, UISearchBarIcon) {
    
        UISearchBarIconSearch, //搜索图标
    
        UISearchBarIconClear, // 清除图标
    
        UISearchBarIconBookmark, // 书本图标
    
        UISearchBarIconResultsList, // 结果列表图标
    
    };
    

    下面是搜索框控件的一些代理方法:

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;           
    
    将要开始编辑时的回调,返回为NO,则不能编辑
    
    - (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 ; 
    
    编辑文字改变前的回调,返回NO则不能加入新的编辑文字
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;       
    
    搜索按钮点击的回调
    
    - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;             
    
    书本按钮点击的回调
    
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;               
    
    取消按钮点击的回调
    
    - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar; 
    
    搜索结果按钮点击的回调
    
    - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;
    
    搜索栏的附加试图中切换按钮触发的回调
    
    D7C05039-1BCD-40EA-801F-F3BD6C8CDE41.png

    相关文章

      网友评论

          本文标题:iOS UISearchBar轻松改变搜索图标和占位符坐标

          本文链接:https://www.haomeiwen.com/subject/tofqtftx.html