alexaubry / alexaubry/BulletinBoard

Adjust tableView height when keyboard appears

Open
#201 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
5.3k
Forks
301
PR merge metrics
No merged PRs in 30d

Description

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](https://user-images.githubusercontent.com/6551927/114619288-a74ac280-9caa-11eb-843c-8874447a54f3.png)
![Simulator Screen Shot - iPod touch (7th generation) - 2021-04-13 at 21 35 30](https://user-images.githubusercontent.com/6551927/114619292-a7e35900-9caa-11eb-863d-b67e596a109f.png)

Thanks for the help anyone can give me 🙏

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.