自定义UISearchBar的背景

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, kTitleBarHeight)];
searchBar.delegate = self;
searchBar.layer.masksToBounds = YES;  //取消searchbar的边框颜色
NSString *osVersion = [DeviceHelper getOSVersion];
NSRange baseVersionNumRange = {0,1};
int baseVersion = [[osVersion substringWithRange:baseVersionNumRange] intValue];
if (baseVersion>4){ //ios5api支持修改
searchBar.backgroundImage = [UIImage imageNamed:@"搜索.png"];
}
else{ //ios 4 一下的系统使用该方法修改背景
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:searchBar.bounds];
backgroundView.image = [UIImage imageNamed:@"搜索.png"];
[searchBar insertSubview:backgroundView atIndex:1]; // at index 1 but not 0
[backgroundView release];
}
tvList.tableHeaderView = searchBar;
[searchBar release];

 

///////////////////////////////

//获取系统版本号

+ (NSString*)getOSVersion
{
return [[UIDevice currentDevice]systemVersion];
}

posted on 2012-11-07 12:07  kiao295338444  阅读(302)  评论(0编辑  收藏  举报

导航