UISearchBar in UITableView header layout issue in iOS 14
Asked Answered
J

1

0

I am using the new UISearchController with searchbar inside tableview header.Everything works fine in portrait orientation but when I rotate to landscape and uisearchbar start editing its frame is out of screen,clipping the cancel button. my code for creating searchcontroller

tableView.tableHeaderView = searchController.searchBar

Here is my sample code UISearchController sample

Rotation has no frame issue in landscape but only when uisearchbar starts editing [landscape searchbar

Cancel button clipping

When rotate back to portrait layout of uisearchbar is still incorrectportrait screen after rotating back There is no issue if I use UITableviewController but I have to implement with uitableview.Is there any setting I am missing in iOS 14 ?

Note:I have to use tableHeaderView not navigationItem.searchController = searchController

Johann answered 29/7, 2021 at 7:18 Comment(4)
May or may not be a solution for you, but... try constraining the tableView Leading and Trailing to superView instead of safe-area.Knowitall
I don't know why you "have to implement with UITableView"? Just use the tools available to you. Why make life difficult for yourself?Applicant
@Applicant uitableview give me more flexibility to add more views to my screen in future.these are old issues that things supporting easily on uitableviewcontroller not working with uitableviewJohann
@Johann you can use view controller containment and let the table view controller control the tableview, and you can still add other views (or view controllers) to your main vc.Applicant
H
-1

Try adjusting the frame of search bar in viewWillTransition method that in your corresponding UIViewController . viewWillTransition is called every time during rotation and enables you to change layout during rotation of your UIViewController.

here is example code:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animate( alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in
        adjustSearchbarFrame()
    }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
    })
    super.viewWillTransition(to: size, with: coordinator)
}
Hakeem answered 29/7, 2021 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.