Incorrect UISearchController position due to table insert

I am working on a function that has a user view on a UITableView, it looks like a TableView has a contentInset. I added a SearchController to the TableViewHeader, but when I click on searchTextView, the SearchController moves up with the animation (about 20 pixels). I want the UISearchBar to be at the top of the ViewController, as in "Constacts"

This screenshot shows how it is now:

enter image description here

this is how i want:

enter image description here

What should I do? I need a contentInset after the search (the user clicked on cancel), but I do not need it during the search.

I want to provide an example application that demonstrates all this -> enter link description here

+6
13

. TableView, , ViewController " " .

                      **OR**

, Under Bottom Bars.

:

option 1

:

option2

+7

Adjust Scroll View Insets, Under Top Bars Under Bottom Bars . collectionView, . , , .

enter image description here

enter image description here

+5

viewWillAppear()

self.automaticallyAdjustsScrollViewInsets = false
+3

, searchBar / :

// MARK: - UISearchBarDelegate
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    UIView.transition(with: self.tableView,
                      duration: 0.6,
                      options: .curveLinear,
                      animations: {
                        self.tableView.contentInset = UIEdgeInsets.zero
    }, completion: { success in })
    return true
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    UIView.transition(with: self.tableView,
                      duration: 0.6,
                      options: .curveLinear,
                      animations: {
                        self.tableView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
    }, completion: { success in })
}
+1
  • UISearchBar tableHeaderView

    searchbar   =   UISearchBar(frame: CGRect(x: 0, y: 0, width: 375, height: 44))
    searchbar?.delegate =   self
    tableView.tableHeaderView = searchbar
    
  • SearchBar UISearchController

    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
    
        if searchBar == searchbar {
    
           self.tableView.tableHeaderView  =   nil
    
           UIView.animate(withDuration: 0.33, animations: {
               // 64+44 is due because the navigation gets hidden and also because of the temp searchBar as tableHeaderView
               self.tableView.contentInset =   UIEdgeInsets(top: 64+44, left: 0, bottom: 0, right: 0)
           }, completion: { _ in
    
           })
           self.present(searchController, animated: true, completion: nil)
       }
    
       return false
    
    }
    
  • , UISearchController searchBar reset tableView contentInset

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        if searchBar == searchController.searchBar {
            UIView.animate(withDuration: 0.33, animations: {
                self.tableView.contentInset =   UIEdgeInsets(top: 100+44, left: 0, bottom: 0, right: 0)
            }, completion: { _ in
    
            })
    
            self.searchbar?.isHidden    =   false
            self.tableView.tableHeaderView  =   self.searchbar
        }
    }
    
+1

, , edgesForExtendedLayout none, , Extend Edges Under Top Bar .

0

, -, , , Objective-C , .

, , . :

  • , , ,
  • , ,
  • , .
  • , , , .

Right?

, , .

, UITableViewController UIViewController. . , , . , , , , - , , , . , , , , , , , , , , UISearchBar .

!

0

, YourVC (VC, SearchBar Table View) NavigationController VC, .

, , .

:

  • (: 0, : 0, : 0, : 60).
  • , Constraint (Top: 20, leading: 0, trailing: 0, bottom: 0).
  • SearchBarStyle searchBar.
  • "" .
  • , , Color Picker.
  • TableView View ( : 0, : 0, : 0, : 0).
  • " ", . Uncheck

, .

0

UITableView .

Adjust Scroll View Insets, . .

0

, tableView ViewController, , , tableView navigationBar .

storyBoard

, - , , searchController, ,

var searchController: UISearchController?

override func viewDidLoad() {
    super.viewDidLoad()

    searchController?.isActive = true
    searchController = initSearchController()
}

func initSearchController() -> UISearchController {
    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.searchBar.delegate = self
    searchController.hidesNavigationBarDuringPresentation = true
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true


    searchController.searchBar.searchBarStyle = .minimal
    searchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchController.searchBar
    tableView.contentOffset = CGPoint(x: 0, y: 
    searchController.searchBar.frame.size.height)
    return searchController
}

,

0

, , . 2 .

  • ,

    self.navigationController.navigationBarHidden = YES;//( c)  self.navigationController.navigationBarHidden = true;//(swift)

  • view, origin.y 0 -44 ( Bar 44), , enter image description here

0

self.extendedLayoutIncludesOpaqueBars, .

0

UISearchBar, UIViewController, UICollectionView, , (UICollectionView), .

, , viewDidLoad viewWillAppear:

        if #available(iOS 11.0, *) {
            self.collectionView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }

:

0
source

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


All Articles