#StackBounty: #ios #swift #uisearchbar #uisearchcontroller #uisearchresultscontroller UISearchBar's height is incorrect when presen…

Bounty: 50

I have a search bar in tableView’s header view, and I don’t change the height of it(default is 56pt). When search bar is active and its position goes top of the screen, it’s height becomes 50pt. It will both happen on iPhone or simulator, version iOS 11.

First appear:

<UISearchBar: 0x7f94b6646900; frame = (0 0; 375 56); text = ''; gestureRecognizers = <NSArray: 0x60400025fc20>; layer = <CALayer: 0x604000225fe0>>

Editing:

<UISearchBar: 0x7f94b6646900; frame = (0 14; 375 50); text = ''; autoresize = W+TM; gestureRecognizers = <NSArray: 0x60400025fc20>; layer = <CALayer: 0x604000225fe0>>

It results in a weird gap between the search bar and result controller’s view.
gap

Thanks for any ideas.

Edit1:
Here’s my code:

fileprivate func configSearchBar() -> Void {
    let bar = self.searchBar
    var size = bar.frame.size

    let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
    bar.setBackgroundImage(UIImage.imageWith(color: .white, size: size),
                           for: .any,
                           barMetrics: .default)
    let colorWhenEditing = UIColor.colorFromHexString("#FAFAFA")
    if UIDevice.isIPHONEX() {
        bar.barTintColor = colorWhenEditing
    } else {
        bar.setBackgroundImage(UIImage.imageWith(color: colorWhenEditing, size: CGSize(width: size.width, height: size.height + statusBarHeight)),
                               for: .topAttached,
                               barMetrics: .default)
    }
    size = CGSize(width: bar.frame.size.width - 30, height: 30)
    let image = UIImage.imageWith(color: UIColor(hexString: "#F0F0F0"), size: size)?.zoom(toSize: size, cornerRadius: 4)
    bar.setSearchFieldBackgroundImage(image, for: .normal)

    bar.searchTextPositionAdjustment = UIOffset(horizontal: 5, vertical: 0)
}

self.tableView.tableHeaderView = self.searchBar

Edit2: – Search controller and search bar initialization:

// Main view controller
lazy var searchResultController: UISearchController = {
    let vc = MailSearchResultViewController(nibName: nil, bundle: nil)
    let sc = UISearchController(searchResultsController: vc)
    sc.searchResultsUpdater = self
    sc.modalPresentationCapturesStatusBarAppearance = true
    sc.delegate = self
    return sc
}()
var searchBar: UISearchBar {
    return searchResultController.searchBar
}
// MailSearchResultViewController - viewDidLoad:
self.edgesForExtendedLayout = []
self.automaticallyAdjustsScrollViewInsets = false


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.