Skip to content

Adjust tableView height when keyboard appears #201

@link60

Description

@link60

Hi!
I created a page containing a searchBar and a tableView.
When the keyboard appears, the page goes up and the top comes out of the screen.
I have tried several things, but I am unable to reduce the size of the tableView so that the search bar stays visible.

Here is my code, simplified, to use and try as is:

import UIKit
import BLTNBoard

class HistoricPage: FeedbackPageBLTNItem {
	
	fileprivate var tableView: UITableView?
	fileprivate var searchBar: UISearchBar?
	
	fileprivate var numberOfItems: Int = 20
	
	override func tearDown() {
		super.tearDown()
		tableView?.dataSource = nil
		tableView?.delegate = nil
		searchBar?.delegate = nil
	}
	
	override func makeViewsUnderTitle(with interfaceBuilder: BLTNInterfaceBuilder) -> [UIView]? {
		let stack = interfaceBuilder.makeGroupStack(spacing: 0)
		stack.alignment = .fill
		stack.distribution = .fill
		
		let searchBar = UISearchBar(frame: .zero)
		searchBar.searchBarStyle = .minimal
		let searchBarWrapper = interfaceBuilder.wrapView(searchBar, width: nil, height: nil, position: .pinnedToEdges)
		self.searchBar = searchBar
		
		let tableView = UITableView(frame: .zero, style: .plain)
		tableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellIdentifier")
		tableView.rowHeight = 38
		let tableWrapper = interfaceBuilder.wrapView(tableView, width: nil, height: NSNumber(value: numberOfItems * Int(tableView.rowHeight)), position: .pinnedToEdges)
		self.tableView = tableView
		
		tableView.dataSource = self
		tableView.delegate = self
		searchBar.delegate = self
		
		stack.addArrangedSubview(searchBarWrapper)
		stack.addArrangedSubview(tableWrapper)
		return [stack]
	}
}

extension HistoricPage: UITableViewDataSource, UITableViewDelegate {
	
	func numberOfSections(in tableView: UITableView) -> Int {
		return 1
	}
	
	func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return numberOfItems
	}
	
	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
		cell.textLabel?.text = String(indexPath.row+1)
		return cell
	}
	
	func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
		self.searchBar?.resignFirstResponder()
	}
}

extension HistoricPage: UISearchBarDelegate {
	
	func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
		// Something to do here to resize tableView/tableWrapper?
	}
	
	func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
		if let text = searchBar.text, let numberOfItems = Int(text) {
			tableView?.setContentOffset(.zero, animated: true) // scroll to top
			self.numberOfItems = numberOfItems
			self.tableView?.reloadData()
			// Something to do here to resize tableView/tableWrapper according to number of items?
		}
	}
}

Simulator Screen Shot - iPod touch (7th generation) - 2021-04-13 at 21 35 20
Simulator Screen Shot - iPod touch (7th generation) - 2021-04-13 at 21 35 30

Thanks for the help anyone can give me 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions