iosiOS 10开发中经常遇到的问题总结

来源:互联网 发布:数据挖掘入门 编辑:程序博客网 时间:2024/05/01 13:22
1.如何手动取消UIDispalaySearchController的取消搜索状态
  1. #pragma mark UISearchDisplayDelegate    
  2. - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller    
  3. {    
  4.     for (UIView *view in controller.searchBar.subviews)    
  5.     {    
  6.          NSLog(@"%d__|---%@",__LINE__,view);    
  7.         for (UIView *subView in view.subviews)    
  8.         {    
  9.              NSLog(@"%d__|!!!%@",__LINE__,subView);    
  10.             //  获取"取消"按钮    
  11.             if([subView isKindOfClass:[UIButton class]])    
  12.             {    
  13.                 UIButton *cancelButton = (UIButton *)subView;    
  14.                 //  获取点击"取消"按钮的响应事件(actionsForTarget 这个方法返回的是一个数组)    
  15.                 self.cancelSearchSELString = [[cancelButton actionsForTarget:controller.searchBar forControlEvent:UIControlEventTouchUpInside] objectAtIndex:0];    
  16.                 //  响应通知,执行方法直接用上面获得的响应事件方法,转换一下(这是个知识点,可以扩展下)    
  17.                 [[NSNotificationCenter defaultCenter] addObserver:controller.searchBar selector:NSSelectorFromString(self.cancelSearchSELString) name:@"cancelSearch" object:nil];    
  18.             }    
  19.         }    
  20.     }    
  21. }    
  22.     
  23. #pragma mark UISearchBarDelegate------点击搜索按钮    
  24. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{    
  25.     
  26.     //  获取你想搜索的最终完整关键字(一般可以用来做搜索历史展示)      
  27.     NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,searchBar.text);    
  28.     //  点击按钮时,发布取消搜索状态通知    
  29.     [[NSNotificationCenter defaultCenter] postNotificationName:@"cancelSearch" object:nil];    
  30.     //  发布---响应---取消通知    
  31.     [[NSNotificationCenter defaultCenter] removeObserver:searchBar name:@"cancelSearch" object:nil];    
  32. }   

  33. 2.如何知道导航栏是pop还是push
    1. - (void)viewWillDisappear:(BOOL)animated  
    2. {  
    3.     [super viewWillDisappear:animated];  
    4.     if ([self isMovingFromParentViewController])  
    5.     {  
    6.         NSLog(@"View controller was popped");  
    7.     }  
    8.     else  
    9.     {  
    10.         NSLog(@"New view controller was pushed");  
    11.     }  
    12. }  

  34. 3.更改导航栏颜色
    1. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {  
    2.   
    3.     // do stuff for iOS 7 and newer  
    4.     [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];  
    5. }  
    6. else {  
    7.   
    8.     // do stuff for older versions than iOS 7  
    9.     [self.navigationController.navigationBar setTintColor:[UIColor yellowColor]];  
    10. }  

  35. 4.如何更改导航栏title文字的颜色
    1. [self.navigationController.navigationBar setTitleTextAttributes:  
    2.  @{NSForegroundColorAttributeName:[UIColor redColor],  
    3. NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}];  

  36. 5.减少多余的tableView空的cell
  37. tableView.tableFooterView = [UIView new];
6.返回CGFloat_MIN
    1. // footer 间距  
    2. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  
    3. {  
    4. //    return 1.0f;  
    5.     return CGFLOAT_MIN;  
    6. }  

  1. 7.减少默认tableView的sectionHeader和Footer的高度,直接设置0是无效的,最小是1.0f
    1. - (CGFloat)tableView:(UITableView*)tableView   
    2.            heightForHeaderInSection:(NSInteger)section {  
    3.     if (section == 0) {  
    4.         return 6.0;  
    5.     }  
    6.   
    7.     return 1.0;  
    8. }  
    9.   
    10. - (CGFloat)tableView:(UITableView*)tableView   
    11.            heightForFooterInSection:(NSInteger)section {  
    12.     return 5.0;  
    13. }  
    14.   
    15. - (UIView*)tableView:(UITableView*)tableView   
    16.            viewForHeaderInSection:(NSInteger)section {  
    17.     return [[UIView alloc] initWithFrame:CGRectZero];  
    18. }  
    19.   
    20. - (UIView*)tableView:(UITableView*)tableView   
    21.            viewForFooterInSection:(NSInteger)section {  
    22.     return [[UIView alloc] initWithFrame:CGRectZero];  
    23. }  

  2. 8.设置中号字体
    1.  UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0f];  
    2.     //iOS8.2开始  
    3.     [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];  

  3.  
0 0
原创粉丝点击