Reputation: 955
in my swift 2 app i have a table view controller with a search bar. i setup the search bar in my viewDidLoad. and in the viewWillAppear i set the offset of my tableView, because i would like to hide the search bar on start. this works finde.
but if I switch to another view controller and go back to the TableViewController with an exit unwind segue, the search bar will be directly visible.
if I go back to my TableViewController from another view controller with an normal modal segue (not unwind), the search bar will be hidden, because the tableview.setContentOffset will be active.
any ideas what i do wrong?
class TableViewController: UITableViewController, ADBannerViewDelegate, UISearchBarDelegate, UISearchResultsUpdating {
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var items = [CoreData_ITEMS]()
func updateSearchResultsForSearchController(searchController: UISearchController) {}
func DatenAbrufen() {
let fetchRequest = NSFetchRequest(entityName: "ITEMS")
do {
try self.items = self.managedObjectContext!.executeFetchRequest(fetchRequest) as! [CoreData_ITEMS]
} catch { }
self.tableView.reloadData()
}
/*************** VIEW DID LOAD ***************/
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
}
override func viewWillAppear(animated: Bool) {
DatenAbrufen()
tableView.contentOffset = CGPointMake(0,44)
}
/*************** ANZAHL DER ZELLEN ERMITTELN ***************/
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
/*************** ZELLEN MIT INHALT FÜLLEN ***************/
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! ModifyCells
var ITEM:CoreData_ITEMS
ITEM = items[indexPath.row]
cell.Name.text = ITEM.name
return cell
}
/*************** EXIT FUNKTIONEN ***************/
@IBAction func exit_to_TViewController(segue: UIStoryboardSegue) {}
}
Upvotes: 0
Views: 472
Reputation: 4522
I had a similar problem. The thing is that tableView.contentOffset
does not get set unless the tableview is displayed on the screen. I don't know why it has this behaviour. So what fixed it for me was moving the tableView.contentOffset
call to vieDidAppear
but then the scrolling is a bit visible. This wasn't a problem for me. If you don't want that you could try moving the call to viewDidLayoutSubviews
.
Upvotes: 0