UISearchController - 空白空间隐藏状态栏

问题描述:

我正在使用UISearchController,它工作正常。但是它会在搜索栏上留下一个空白区域(聚焦时),这部分隐藏了状态栏。 This espaceUISearchController - 空白空间隐藏状态栏

我已经尝试设置self.edgesForExtendedLayout = UIRectEdgeNoneself.navigationController.extendedLayoutIncludesOpaqueBars = YES但空间仍然存在。我尝试设置self.definesPresentationContext = YES;,但它生成了searchBar采取状态栏位置like this

我使用嵌入在UINavigationController中的UIViewController。这是我现在执行的设置:

@interface DirectoryViewController : UIViewController <UITableViewDataSource , UITableViewDelegate ,UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating> 

@property (strong, nonatomic) NSMutableArray *personsArray; 
@property (nonatomic, strong) UISearchController *searchController; 
@property (nonatomic, strong) NSMutableArray *searchResults; 


@implementation DirectoryViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
UINavigationController *searchResultsController = [[self storyboard] instantiateViewControllerWithIdentifier:@"TableSearchResultsNavController"]; 

    // Our instance of UISearchController will use searchResults 
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController]; 

    // The searchcontroller's searchResultsUpdater property will contain our tableView. 
    self.searchController.searchResultsUpdater = self; 
    self.searchController.delegate = self; 
    self.searchController.searchBar.delegate = self; 

    //Cancel button tint 
    _searchController.searchBar.tintColor = [UIColor whiteColor]; 
    [self.searchController.searchBar setBackgroundImage:[UIImage imageNamed:@"navBar"] 
               forBarPosition:0 
                barMetrics:UIBarMetricsDefault]; 

    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) { /// iOS 7 or above 
     self.edgesForExtendedLayout = UIRectEdgeNone; 
    } 

    self.navigationController.extendedLayoutIncludesOpaqueBars = YES; 

} 

我很感激任何帮助。谢谢

我在This answer找到解决方案。

更改tableInsets:

self.directoryTable.contentInset = UIEdgeInsetsMake(-10, 0, 0, 0); 

当searchController被becameFirstResponder

self.directoryTable.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); 

当搜索栏动作结束。