Skip to content

Instantly share code, notes, and snippets.

@bricklife
Last active May 9, 2018 05:25
Show Gist options
  • Save bricklife/68a04a6171063c8429a9 to your computer and use it in GitHub Desktop.
Save bricklife/68a04a6171063c8429a9 to your computer and use it in GitHub Desktop.
Resizable inputAccessoryView on UITableView in iOS 8
import UIKit
class InputAccessoryTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.becomeFirstResponder()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
cell.textLabel?.text = "\(indexPath.row + 1)"
return cell
}
}
class InputAccessoryTableView: UITableView, UITextViewDelegate {
var accessoryView: UIView!
override var inputAccessoryView: UIView? {
get {
if self.accessoryView == nil {
self.accessoryView = UIView(frame: CGRectMake(0, 0, 320, 46))
self.accessoryView.backgroundColor = UIColor.groupTableViewBackgroundColor()
let textView = UITextView(frame: CGRectMake(8, 8, 304, 30))
textView.translatesAutoresizingMaskIntoConstraints = false
textView.delegate = self
self.accessoryView.addSubview(textView)
let viewBindingsDict = ["textView" : textView]
self.accessoryView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-8-[textView]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewBindingsDict))
self.accessoryView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[textView]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewBindingsDict))
}
return self.accessoryView
}
}
override func canBecomeFirstResponder() -> Bool {
return true
}
func textViewDidChange(textView: UITextView) {
print(textView.contentSize)
for constraint in self.accessoryView.constraints where constraint.firstAttribute == .Height {
print(constraint)
constraint.constant = textView.contentSize.height + 16
}
//self.reloadInputViews()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment