Scrolling tableview when keyboard appears for UITextView
up vote
0
down vote
favorite
I know this is an often asked question, but I cannot get any of the posted solutions (e.g. here) to work properly in my case. First off, when I handle keyboardWillShow notification using
if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
}
nothing happens. It seems like a bit of a hack, but this implementation below (specifically in keyboardWillShow) works for me, however strangely (1) it doesn't work the first time but works every subsequent time also (2) a large white bar appears above the keyboard for some reason? I don't think it matters but my UI lets the user tap an edit button so they can see what is editable, then they edit the textView, then tap done. The problem I'm trying to solve is that this textView is at the bottom of the tableView, so the keyboard obscures it while editing.
class ScoreAndStatsViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate {
@IBOutlet weak var editButton: UIButton!
@IBOutlet weak var notesTextField: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIApplication.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIApplication.keyboardWillHideNotification, object: nil)
}
@IBAction func editButtonIsPressed(_ sender: UIButton) {
if editMode == false {
notesTextField.isEditable = true
notesTextField.backgroundColor = iPhoneForeGroundColor
editButton.setTitle("Done", for: .normal)
self.editMode = true
//If edit mode is true, this means they've hit the done button so save
} else {
//save data
editButton.setTitle("Edit", for: .normal)
notesTextField.isEditable = false
notesTextField.backgroundColor = UIColor.clear
self.editMode = false
}
}
// MARK: Keyboard Notifications
@objc func keyboardWillShow(notification: NSNotification) {
let pointInTable:CGPoint = notesTextField.superview!.convert(notesTextField.frame.origin, to: tableView)
var contentOffset:CGPoint = tableView.contentOffset
contentOffset.y = pointInTable.y
if let accessoryView = tableView.inputAccessoryView {
contentOffset.y -= accessoryView.frame.size.height
}
tableView.contentOffset = contentOffset
}
@objc func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: 0.2, animations: {
// For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
})
}
}
ios swift uitableview uiscrollview uitextview
add a comment |
up vote
0
down vote
favorite
I know this is an often asked question, but I cannot get any of the posted solutions (e.g. here) to work properly in my case. First off, when I handle keyboardWillShow notification using
if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
}
nothing happens. It seems like a bit of a hack, but this implementation below (specifically in keyboardWillShow) works for me, however strangely (1) it doesn't work the first time but works every subsequent time also (2) a large white bar appears above the keyboard for some reason? I don't think it matters but my UI lets the user tap an edit button so they can see what is editable, then they edit the textView, then tap done. The problem I'm trying to solve is that this textView is at the bottom of the tableView, so the keyboard obscures it while editing.
class ScoreAndStatsViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate {
@IBOutlet weak var editButton: UIButton!
@IBOutlet weak var notesTextField: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIApplication.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIApplication.keyboardWillHideNotification, object: nil)
}
@IBAction func editButtonIsPressed(_ sender: UIButton) {
if editMode == false {
notesTextField.isEditable = true
notesTextField.backgroundColor = iPhoneForeGroundColor
editButton.setTitle("Done", for: .normal)
self.editMode = true
//If edit mode is true, this means they've hit the done button so save
} else {
//save data
editButton.setTitle("Edit", for: .normal)
notesTextField.isEditable = false
notesTextField.backgroundColor = UIColor.clear
self.editMode = false
}
}
// MARK: Keyboard Notifications
@objc func keyboardWillShow(notification: NSNotification) {
let pointInTable:CGPoint = notesTextField.superview!.convert(notesTextField.frame.origin, to: tableView)
var contentOffset:CGPoint = tableView.contentOffset
contentOffset.y = pointInTable.y
if let accessoryView = tableView.inputAccessoryView {
contentOffset.y -= accessoryView.frame.size.height
}
tableView.contentOffset = contentOffset
}
@objc func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: 0.2, animations: {
// For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
})
}
}
ios swift uitableview uiscrollview uitextview
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Tobi
Nov 18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 at 14:18
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I know this is an often asked question, but I cannot get any of the posted solutions (e.g. here) to work properly in my case. First off, when I handle keyboardWillShow notification using
if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
}
nothing happens. It seems like a bit of a hack, but this implementation below (specifically in keyboardWillShow) works for me, however strangely (1) it doesn't work the first time but works every subsequent time also (2) a large white bar appears above the keyboard for some reason? I don't think it matters but my UI lets the user tap an edit button so they can see what is editable, then they edit the textView, then tap done. The problem I'm trying to solve is that this textView is at the bottom of the tableView, so the keyboard obscures it while editing.
class ScoreAndStatsViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate {
@IBOutlet weak var editButton: UIButton!
@IBOutlet weak var notesTextField: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIApplication.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIApplication.keyboardWillHideNotification, object: nil)
}
@IBAction func editButtonIsPressed(_ sender: UIButton) {
if editMode == false {
notesTextField.isEditable = true
notesTextField.backgroundColor = iPhoneForeGroundColor
editButton.setTitle("Done", for: .normal)
self.editMode = true
//If edit mode is true, this means they've hit the done button so save
} else {
//save data
editButton.setTitle("Edit", for: .normal)
notesTextField.isEditable = false
notesTextField.backgroundColor = UIColor.clear
self.editMode = false
}
}
// MARK: Keyboard Notifications
@objc func keyboardWillShow(notification: NSNotification) {
let pointInTable:CGPoint = notesTextField.superview!.convert(notesTextField.frame.origin, to: tableView)
var contentOffset:CGPoint = tableView.contentOffset
contentOffset.y = pointInTable.y
if let accessoryView = tableView.inputAccessoryView {
contentOffset.y -= accessoryView.frame.size.height
}
tableView.contentOffset = contentOffset
}
@objc func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: 0.2, animations: {
// For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
})
}
}
ios swift uitableview uiscrollview uitextview
I know this is an often asked question, but I cannot get any of the posted solutions (e.g. here) to work properly in my case. First off, when I handle keyboardWillShow notification using
if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
}
nothing happens. It seems like a bit of a hack, but this implementation below (specifically in keyboardWillShow) works for me, however strangely (1) it doesn't work the first time but works every subsequent time also (2) a large white bar appears above the keyboard for some reason? I don't think it matters but my UI lets the user tap an edit button so they can see what is editable, then they edit the textView, then tap done. The problem I'm trying to solve is that this textView is at the bottom of the tableView, so the keyboard obscures it while editing.
class ScoreAndStatsViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate {
@IBOutlet weak var editButton: UIButton!
@IBOutlet weak var notesTextField: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIApplication.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIApplication.keyboardWillHideNotification, object: nil)
}
@IBAction func editButtonIsPressed(_ sender: UIButton) {
if editMode == false {
notesTextField.isEditable = true
notesTextField.backgroundColor = iPhoneForeGroundColor
editButton.setTitle("Done", for: .normal)
self.editMode = true
//If edit mode is true, this means they've hit the done button so save
} else {
//save data
editButton.setTitle("Edit", for: .normal)
notesTextField.isEditable = false
notesTextField.backgroundColor = UIColor.clear
self.editMode = false
}
}
// MARK: Keyboard Notifications
@objc func keyboardWillShow(notification: NSNotification) {
let pointInTable:CGPoint = notesTextField.superview!.convert(notesTextField.frame.origin, to: tableView)
var contentOffset:CGPoint = tableView.contentOffset
contentOffset.y = pointInTable.y
if let accessoryView = tableView.inputAccessoryView {
contentOffset.y -= accessoryView.frame.size.height
}
tableView.contentOffset = contentOffset
}
@objc func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: 0.2, animations: {
// For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
})
}
}
ios swift uitableview uiscrollview uitextview
ios swift uitableview uiscrollview uitextview
asked Nov 18 at 14:08
GarySabo
1,36111643
1,36111643
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Tobi
Nov 18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 at 14:18
add a comment |
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Tobi
Nov 18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 at 14:18
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Tobi
Nov 18 at 14:14
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Tobi
Nov 18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 at 14:18
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 at 14:18
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53361784%2fscrolling-tableview-when-keyboard-appears-for-uitextview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Tobi
Nov 18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 at 14:18