Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust tableView height when keyboard appears #201

Open
link60 opened this issue Apr 13, 2021 · 2 comments
Open

Adjust tableView height when keyboard appears #201

link60 opened this issue Apr 13, 2021 · 2 comments

Comments

@link60
Copy link

link60 commented Apr 13, 2021

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 🙏

@link60
Copy link
Author

link60 commented Apr 13, 2021

I finally found a solution that suits me:

page.shouldRespondToKeyboardChanges = false

and basically :

    override func onDisplay() {
        super.onDisplay()
        NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardHide), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    @objc func onKeyboardShow(notification: NSNotification) {
        if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
            self.tableView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
        }
    }
    
    @objc func onKeyboardHide(notification: NSNotification) {
        UIView.animate(withDuration: 0.2, animations: {
            self.tableView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 88, right: 0)
        })
    }

@CarlosEVC63
Copy link

Thank you for publishing the problem and the solution.
It has been a very good insight for my project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants