9月份問(wèn)題總結(jié):
主要內(nèi)容:
(1) 適配iOS11,Xcode 9中出現(xiàn)問(wèn)題歸納.
(2) Tableview預(yù)加載問(wèn)題測(cè)試
一.適配iOS 11; UISearchBar / UISearchBar導(dǎo)航欄顯示問(wèn)題
(1)iOS 11導(dǎo)航欄顯示效果改變
標(biāo)注1: UISearchBar背景高度改為56
標(biāo)注2: UISearchBar內(nèi)部TextField高度改為36
嘗試解決方法:
(1) http://www.reibang.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)
//調(diào)用方法
- (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;
}
快速方法顯示效果:
備注::不是放在導(dǎo)航欄上UISearchBar,調(diào)用相同方法即可.(背景高度改變-可添加約束/放在父view上等等)
二.適配iOS 11; Safe Area Layout Guide Before IOS 9.0
(1)方法:
http://blog.csdn.net/chmod_r_755/article/details/78052380
三.Tableview滑動(dòng)預(yù)加載測(cè)試:
(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實(shí)現(xiàn)連續(xù)搜索界面,支持/熱搜/歷史搜索記錄.去重/清楚歷史搜索/篩選過(guò)濾/;整體效果類(lèi)似京東首頁(yè)搜索效果
(1)主要代碼:
1. 保存搜索記錄:
//保存搜索記錄
-(void)SearchText :(NSString *)seaTxt{
NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
//讀取數(shù)組NSArray類(lèi)型的數(shù)據(jù)
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;
//判斷搜索內(nèi)容是否存在,存在的話(huà)放到數(shù)組第一位,不存在的話(huà)添加寥裂。
for (NSString * str in myArray) {
if ([seaTxt isEqualToString:str]) {
//獲取指定對(duì)象的索引
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去掉最后一個(gè),保存15條記錄
if(searTXT.count > 15){
[searTXT removeObjectAtIndex:searTXT.count-1];
}
//將上述數(shù)據(jù)全部存儲(chǔ)到NSUserDefaults中
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:searTXT forKey:@"searchHistory"];
}
2. 過(guò)濾搜索數(shù)據(jù):
NSString *searchString = [self.searBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.filterResultArray!= nil) {
[self.filterResultArray removeAllObjects];
}
//過(guò)濾數(shù)據(jù)
self.filterResultArray = [NSMutableArray arrayWithArray:[self.filterArray filteredArrayUsingPredicate:preicate]];
3. 實(shí)現(xiàn)效果:
Demo地址:https://github.com/FTC-Chen/SearchWithHotHistory
五.參考資料
1.http://blog.csdn.net/shengdavolleyball/article/details/50751970
有任何問(wèn)題請(qǐng)私信或者留言.