iOS 9 searchBar disappears from table header view when UISearchController is active

后端 未结 13 1280
無奈伤痛
無奈伤痛 2020-12-07 20:37

The structure:

View1 (click a button) -> present modally (MyModalView: UITableViewController)

MyModalView has UISearchController embedded. The searchBar of U

相关标签:
13条回答
  • 2020-12-07 21:06

    I fixed it in my case by removing

    definesPresentationContext = true

    I didn't test yet if there are any disadvantages of removing this!

    0 讨论(0)
  • 2020-12-07 21:07

    I had to

    self.aNavigationController?.extendedLayoutIncludesOpaqueBars = true
    

    I found a similar question here but in my case it was not on the viewDidLoad method. I had to try different views until it worked. Now I can have both a custom navigation bar color and the search bar,

    0 讨论(0)
  • 2020-12-07 21:08

    I'm not sure what exactly is the problem but I 'fixed' it by:

    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.definesPresentationContext = NO;
    

    My guess is that UISearchController is doing something funky when it is trying to present as a navigation bar. So, this is a hack but it at least doesn't block the user. The search bar doesn't do the cool animation and cover up the navigation bar.

    0 讨论(0)
  • 2020-12-07 21:12

    I don't have a navigation bar in this place of an app. None of other SO posts helped me, so I've fixed it this way:

    - (void)layoutSubviews
    {
        [[[self searchController] searchBar] sizeToFit];
    }
    
    0 讨论(0)
  • 2020-12-07 21:13

    I found it's the simulated metrics (top bar) in storyboard that's cause this problem. In my case, the following lines work, but I still don't know why.

    - (void)willPresentSearchController:(UISearchController *)searchController {
        // do something before the search controller is presented
        self.navigationController.navigationBar.translucent = YES;
    }
    
    -(void)willDismissSearchController:(UISearchController *)searchController
    {
        self.navigationController.navigationBar.translucent = NO;
    }
    
    0 讨论(0)
  • 2020-12-07 21:13

    I had the same problem, and when I debugged the UI on Xcode I found that the UISearchBar view was moved to another view and the width was zeroed.

    I fixed it by setting definesPresentationContext property of the UISearchController to false, and setting it true for the containing UITableViewController.

    I added only one line to your viewDidLoad().

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Dynamically create a search controller using anonymous function
        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.definesPresentationContext = false    // Disable the presentation controller
    
            controller.searchBar.sizeToFit()
            controller.searchBar.delegate = self
    
            self.tableView.tableHeaderView = controller.searchBar
    
            return controller
        })()
    
        // Auto sizing row & cell height
        self.tableView.estimatedRowHeight = 130
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.definesPresentationContext = true    // This one remains the same
    
        // No footer for better presentation
        self.tableView.tableFooterView = UIView.init(frame: CGRectZero)
    }
    
    0 讨论(0)
提交回复
热议问题