Scrolling tableview when keyboard appears for 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
add a comment |
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 ?
– Mohmmad S
Nov 18 '18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 '18 at 14:18
add a comment |
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 '18 at 14:08
GarySaboGarySabo
1,42711744
1,42711744
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Mohmmad S
Nov 18 '18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 '18 at 14:18
add a comment |
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Mohmmad S
Nov 18 '18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 '18 at 14:18
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Mohmmad S
Nov 18 '18 at 14:14
cocoapods.org/pods/IQKeyboardManagerSwift could this be helpful ?
– Mohmmad S
Nov 18 '18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 '18 at 14:18
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 '18 at 14:18
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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 ?
– Mohmmad S
Nov 18 '18 at 14:14
@Tobi appreciate it but trying to avoid 3rd party dependencies for sort of trivial things like this 😃
– GarySabo
Nov 18 '18 at 14:18