IOS 10開發中經常遇到的問題總結。
1.如何手動取消UIDispalaySearchController的取消搜索狀態
#pragmamarkUISearchDisplayDelegate
-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController*)controller
{
for(UIView*viewincontroller.searchBar.subviews)
{
NSLog(@"%d__|---%@",__LINE__,view);
for(UIView*subViewinview.subviews)
{
NSLog(@"%d__|!!!%@",__LINE__,subView);
//獲取"取消"按鈕
if([subViewisKindOfClass:[UIButtonclass]])
{
UIButton*cancelButton=(UIButton*)subView;
//獲取點擊"取消"按鈕的響應事件(actionsForTarget這個方法返回的是一個數組)
self.cancelSearchSELString=[[cancelButtonactionsForTarget:controller.searchBarforControlEvent:UIControlEventTouchUpInside]objectAtIndex:0];
//響應通知,執行方法直接用上面獲得的響應事件方法,轉換一下(這是個知識點,可以擴展下)
[[NSNotificationCenterdefaultCenter]addObserver:controller.searchBarselector:NSSelectorFromString(self.cancelSearchSELString)name:@"cancelSearch"object:nil];
}
}
}
}
#pragmamarkUISearchBarDelegate------點擊搜索按鈕
-(void)searchBarSearchButtonClicked:(UISearchBar*)searchBar{
//獲取你想搜索的最終完整關鍵字(一般可以用來做搜索歷史展示)
NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,searchBar.text);
//點擊按鈕時,發布取消搜索狀態通知
[[NSNotificationCenterdefaultCenter]postNotificationName:@"cancelSearch"object:nil];
//發布---響應---取消通知
[[NSNotificationCenterdefaultCenter]removeObserver:searchBarname:@"cancelSearch"object:nil];
}
2.如何知道導航欄是pop還是push
-(void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:animated];
if([selfisMovingFromParentViewController])
{
NSLog(@"Viewcontrollerwaspopped");
}
else
{
NSLog(@"Newviewcontrollerwaspushed");
}
}
3.更改導航欄顏色
if(NSFoundationVersionNumber>NSFoundationVersionNumber_iOS_6_1){
//dostuffforiOS7andnewer
[self.navigationController.navigationBarsetBarTintColor:[UIColoryellowColor]];
}
else{
//dostuffforolderversionsthaniOS7
[self.navigationController.navigationBarsetTintColor:[UIColoryellowColor]];
}
4.如何更改導航欄title文字的顏色
[self.navigationController.navigationBarsetTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColorredColor],
NSFontAttributeName:[UIFontfontWithName:@"mplus-1c-regular"size:21]}];
5.減少多余的tableView空的cell
tableView.tableFooterView= [UIView new];
6.返回CGFloat_MIN
//footer間距
-(CGFloat)tableView:(UITableView*)tableViewheightForFooterInSection:(NSInteger)section
{
//return1.0f;
returnCGFLOAT_MIN;
}
7.減少默認tableView的sectionHeader和Footer的高度,直接設置0是無效的,最小是1.0f
-(CGFloat)tableView:(UITableView*)tableView
heightForHeaderInSection:(NSInteger)section{
if(section==0){
return6.0;
}
return1.0;
}
-(CGFloat)tableView:(UITableView*)tableView
heightForFooterInSection:(NSInteger)section{
return5.0;
}
-(UIView*)tableView:(UITableView*)tableView
viewForHeaderInSection:(NSInteger)section{
return[[UIViewalloc]initWithFrame:CGRectZero];
}
-(UIView*)tableView:(UITableView*)tableView
viewForFooterInSection:(NSInteger)section{
return[[UIViewalloc]initWithFrame:CGRectZero];
}
8.設置中號字體
UIFont*font=[UIFontfontWithName:@"HelveticaNeue-Medium"size:14.0f];
//iOS8.2開始
[UIFontsystemFontOfSize:14weight:UIFontWeightMedium];