Position the UISearchBar programmatically using the UISearchController

I am trying to position my UISearchBar directly below my UINavigationBar, but I am having problems when it does not appear at all. I am using iOS8 new searchController.

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
 self.searchController.searchResultsUpdater = self;
 self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
 self.salesTableView.tableHeaderView = self.searchController.searchBar;

I am currently using what correctly adds it to the UITableView header, but the problem is that it scrolls the table, which is not what I want. I want it to be above my desk, so it was used instead and tried it, but it seems like it has disappeared now if I do not use the value right?

 self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
 self.searchController.searchResultsUpdater = self;
 self.searchController.searchBar.frame = CGRectMake(0, 0, self.searchController.searchBar.frame.size.width, 44.0);
 //  self.salesTableView.tableHeaderView = self.searchController.searchBar;
+1
source share
2 answers

(0,0), . , :

self.searchController.searchBar.frame = CGRectMake(0, 64, self.searchController.searchBar.frame.size.width, 44.0);

. ( ): UIView ViewController searchBarView; self.searchController.searchBar searchBarView; searchBarView ViewController.

UIView *searchBarView = CGRectMake(0, 64, self.searchController.searchBar.frame.size.width, 44);
[searchBarView addSubView:self.searchController.searchBar];
[self.view addSubView:searchBarView];

, self.searchController.searchBar.sizeToFit() viewDidload. , ( , , ):

- (void)viewDidLayoutSubviews {
    // you could check
    // if (IS_IPAD || (IS_IPHONE_6PLUS && UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))) 
    [self.searchController.searchBar sizeToFit];
}

stackoverflow. , ^^.

+5

, :

  • automaticallyAdjustsScrollViewInsets false UIViewController. .

  • searchFieldBackgroundPositionAdjustment ( iOS 5+), UISearchBar


:

let searchBar = UISearchBar()
searchBar.searchFieldBackgroundPositionAdjustment = UIOffset(horizontal: 0, vertical: 10)
+1

Source: https://habr.com/ru/post/1687464/