源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

iOS中的UISearchBar搜索框组件基础使用指南

  • 时间:2021-01-09 21:19 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:iOS中的UISearchBar搜索框组件基础使用指南
UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle、text、placeholder等等。但是这些属性显然不足矣满足我们的开发需求。比如:修改placeholder的颜色、修改UISearchBar上面的UITextfield的背景颜色、修改UITextfield上面的照片等等。 为了实现上述的需求,最好写一个UISearchBar的子类就叫LSSearchBar吧 LSSearchBar.h如下:
[u]复制代码[/u] 代码如下:
#import <UIKit/UIKit.h> @interface LSSearchBar : UISearchBar @end
LSSearchBar.m如下:
[u]复制代码[/u] 代码如下:
#import "LSSearchBar.h" @implementation LSSearchBar - (void)layoutSubviews {     [super layoutSubviews];     //通过遍历self.subviews找到searchField     UITextField *searchField;     NSUInteger numViews = [self.subviews count];     for(int i = 0; i < numViews; i++) {         if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {             searchField = [self.subviews objectAtIndex:i];         }     }     //如果上述方法找不到searchField,那就试试下面的方法吧     if (searchField ==  nil) {         NSArray *arraySub = [self subviews];         UIView *viewSelf = [arraySub objectAtIndex:0];         NSArray *arrayView = [viewSelf subviews];         for(int i = 0; i < arrayView.count; i++) {             if([[arrayView objectAtIndex:i] isKindOfClass:[UITextField class]]) {                 searchField = [arrayView objectAtIndex:i];             }         }     }     if(!(searchField == nil)) {         //设置颜色         searchField.textColor = [UIColor whiteColor];         //设置背景颜色         [searchField setBackground: [UIImage imageNamed:@"searchbar"] ];         [searchField setBorderStyle:UITextBorderStyleNone];         //设置placeholder的颜色         [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];         //设置searchField上的照片         UIImage *image = [UIImage imageNamed:@"search"];         UIImageView *iView = [[UIImageView alloc] initWithImage:image];         iView.frame = CGRectMake(0, 0, 15, 15);         searchField.leftView = iView;     } } @end
[b]修改UISearchBar背景颜色 [/b]ISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性。方法是直接将 UISearchBarBackGround移去 
[u]复制代码[/u] 代码如下:
seachBar=[[UISearchBar alloc] init];  seachBar.backgroundColor=[UIColor clearColor];  for (UIView *subview in seachBar.subviews){        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])  {            [subview removeFromSuperview];        break;      }   }
[b]UISearchBar文字颜色改变 [/b]1. 在iOS的7访问文本字段,你必须在水平重申更多。更改您的代码像这样
[u]复制代码[/u] 代码如下:
for (UIView *subView in self.searchBar.subviews) {  for (UIView *secondLevelSubview in subView.subviews){   if ([secondLevelSubview isKindOfClass:[UITextField class]])   {    UITextField *searchBarTextField = (UITextField *)secondLevelSubview;    //set font color here    searchBarTextField.textColor = [UIColor blackColor];    break;   }  } }
或可以设置的tintcolor适用于关键在search bar。 使用tintColor至着色前景 使用barTintColor要着色的栏背景。 在iOS系统V7.0,的UIView的子类派生的基类行为tintColor。见tintColor在为UIView的水平 苹果文件 2. 可以通过设置文字的颜色
[u]复制代码[/u] 代码如下:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
3. 虽然这是真的,UIAppearance协议是一个“公开的API,”这不是真的,UITextField的支持这一点。 如果你看一看UITextField.h并查找字符串“UI_APPEARANCE_SELECTOR”,你会看到它有这个字符串的任何实例。如果你看的UIButton CodeGo.net,你会发现不少-这些都是由该UIAppearance API正式支持的属性。这是众所周知的,UITextField的是不支持的UIAppearance API,所以在桑迪普的答案代码并不总是可行的,它实际上不是最好的方法。 这是帖子的链接: 正确的做法是-遍历子视图(或子视图主要用于IOS7的子视图)和手动设置。否则,您将有不可靠的结果。但你可以创建一个类别的UISearchBar并添加setTextColor:(*的UIColor)示例:
[u]复制代码[/u] 代码如下:
- (void)setTextColor:(UIColor*)color {  for (UIView *v in self.subviews)  {   if([Environment isVersion7OrHigher]) //checks UIDevice#systemVersion      {    for(id subview in v.subviews)    {     if ([subview isKindOfClass:[UITextField class]])     {      ((UITextField *)subview).textColor = color;     }    }   }   else   {    if ([v isKindOfClass:[UITextField class]])    {     ((UITextField *)v).textColor = color;    }   }  } }
[b]自定义UISearchBar的背景图 [/b]
[u]复制代码[/u] 代码如下:
- (void)layoutSubviews {     UITextField *searchField;     NSUInteger numViews = [self.subviews count];     for(int i = 0; i < numViews; i++) {         if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {             searchField = [self.subviews objectAtIndex:i];         }     }     if(!(searchField == nil)) {         searchField.textColor = [UIColor whiteColor];         [searchField.leftView setHidden:YES];         [searchField setBackground: [UIImage imageNamed:@"SearchBarBackground.png"] ];         [searchField setBorderStyle:UITextBorderStyleNone];     }           [super layoutSubviews]; }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部