9月份问题总结:
主要内容:
(1) 适配iOS11,Xcode 9中出现问题归纳.
(2) Tableview预加载问题测试
一.适配iOS 11; UISearchBar / UISearchBar导航栏显示问题
(1)iOS 11导航栏显示效果改变
标注1: UISearchBar背景高度改为56
标注2: UISearchBar内部TextField高度改为36
尝试解决方法:
(1) http://www.jianshu.com/p/39a5aee18778 (更改或自定义)
(2) 快速方法:
[[UISearchBar appearance] setSearchFieldBackgroundImage:[self searchFieldBackgroundImage] forState:UIControlStateNormal];
UITextField *txfSearchField = [self.searchBar valueForKey:@"_searchField"];
[txfSearchField setDefaultTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.5]}];
self.searchBar.searchTextPositionAdjustment = UIOffsetMake(7, 0)
//调用方法
- (UIImage*)searchFieldBackgroundImage {
UIColor*color = [UIColor whiteColor];
CGFloat cornerRadius = 5;
CGRect rect =CGRectMake(0,0,28,28);
UIBezierPath*roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
roundedRect.lineWidth=0;
UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0f);
[color setFill];
[roundedRect fill];
[roundedRect stroke];
[roundedRect addClip];
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
快速方法显示效果:
备注::不是放在导航栏上UISearchBar,调用相同方法即可.(背景高度改变-可添加约束/放在父view上等等)
二.适配iOS 11; Safe Area Layout Guide Before IOS 9.0
(1)方法:
http://blog.csdn.net/chmod_r_755/article/details/78052380
三.Tableview滑动预加载测试:
(1)方法:
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;//边缘距离
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
NSLog(@"offset: %f", offset.y);
NSLog(@"content.height: %f", size.height);
NSLog(@"bounds.height: %f", bounds.size.height);//603,界面高度6s-667-64,
NSLog(@"inset.top: %f", inset.top);
NSLog(@"inset.bottom: %f", inset.bottom);
NSLog(@"pos: %f of %f", y, h);
float reload_distance = 80;
if(y > h - reload_distance) {
NSLog(@"load more rows");
self.pageIndex ++;
[self getServerData];
}
}
Demo地址:https://github.com/FTC-Chen/TableViewPreload
四.使用UISearchBar实现连续搜索界面,支持/热搜/历史搜索记录.去重/清楚历史搜索/筛选过滤/;整体效果类似京东首页搜索效果
(1)主要代码:
1. 保存搜索记录:
//保存搜索记录
-(void)SearchText :(NSString *)seaTxt{
NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
//读取数组NSArray类型的数据
NSArray *myArray = [[NSArray alloc] initWithArray:[userDefaultes arrayForKey:@"searchHistory"]];
// NSArray --> NSMutableArray
NSMutableArray *searTXT = [NSMutableArray array];
searTXT = [myArray mutableCopy];
BOOL isEqualTo1,isEqualTo2;
isEqualTo1 = NO;
isEqualTo2 = NO;
if (searTXT.count > 0) {
isEqualTo2 = YES;
//判断搜索内容是否存在,存在的话放到数组第一位,不存在的话添加。
for (NSString * str in myArray) {
if ([seaTxt isEqualToString:str]) {
//获取指定对象的索引
NSUInteger index = [myArray indexOfObject:seaTxt];
[searTXT removeObjectAtIndex:index];
[searTXT insertObject:seaTxt atIndex:0];
isEqualTo1 = YES;
break;
}
}
}
if (!isEqualTo1 || !isEqualTo2) {
[searTXT insertObject:seaTxt atIndex:0];
//[searTXT addObject:seaTxt];
}
//大于15去掉最后一个,保存15条记录
if(searTXT.count > 15){
[searTXT removeObjectAtIndex:searTXT.count-1];
}
//将上述数据全部存储到NSUserDefaults中
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:searTXT forKey:@"searchHistory"];
}
2. 过滤搜索数据:
NSString *searchString = [self.searBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.filterResultArray!= nil) {
[self.filterResultArray removeAllObjects];
}
//过滤数据
self.filterResultArray = [NSMutableArray arrayWithArray:[self.filterArray filteredArrayUsingPredicate:preicate]];
3. 实现效果:
Demo地址:https://github.com/FTC-Chen/SearchWithHotHistory
五.参考资料
1.http://blog.csdn.net/shengdavolleyball/article/details/50751970
有任何问题请私信或者留言.