I have a UITableViewController with around 20 static cells, some of these cells have UITextFields within them and some are just for selecting with a checkmark. The table is about 1.5 views worth so scrolling is required to get at the lower text fields.
When I click within a textfield at the bottom of the table the keyboard pops up as it should but this then appears over the cell/textfield.
I was under the impression (From Apple docs and elsewhere) that the UITableViewController class handles scrolling of the view automatically when a keyboard appears in any orientation and shifts the tableview up so that the cell is visible, this isn't happening though.
IOS 5.1, iPad Portrait.
Make sure that if you are overriding viewWillAppear that you call
[super viewWillAppear:animated];
If you don't, the Scroll View will not scroll up properly.
Swift
super.viewWillAppear(animated)
I found non of these answers to be correct. After a while, I notice that if you push a controller it won't work ... but if you present it modally.. the table will automatically scroll to the used textfield.
Hope that saves time and stress to anyone.
If the auto scroll of UITableViewController doesn't work with the
UITextFields in cells or scroll weirdly, do these steps. Swift 5 iOS
13.2 tested 100%
First implement viewWillAppear but don't call super.viewWillAppear (this will stop auto scroll)
override func viewWillAppear(_ animated: Bool) {
}
Then let's do the scroll manually.
override func viewWillAppear(_ animated: Bool) {
//Notification center observers
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)),
name: UIResponder.keyboardDidShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)),
name: UIResponder.keyboardWillHideNotification, object: nil)
}
//keybord show action
#objc func keyboardWillShow(notification: Notification) {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: notification.getKeyBoardHeight, right: 0)
}
//keyboard hide action
#objc func keyboardWillHide(notification: Notification) {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
extension Notification {
var getKeyBoardHeight: CGFloat {
let userInfo: NSDictionary = self.userInfo! as NSDictionary
let keyboardFrame: NSValue = userInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
return keyboardRectangle.height
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
I ran into this issue myself. I just converted my view controller from a UIViewController to a UITableViewController in addition to adding the [super viewWillAppear:animated]; call, you will need to remove these lines:
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
As they are no longer needed and setDelegate interferes with the keyboard scrolling behavior.
Related
So I have a UIView which I want to move up and down as the textfield within it is editing and dismissing (keyboard appears and hides). Here is my keyboard observers, and the UIView's default constraint, all inside the viewDidLoad:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIWindow.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIWindow.keyboardWillHideNotification, object: nil)
let radiusViewConstraint = NSLayoutConstraint(item: searchRadiusView!, attribute: .bottom, relatedBy: .equal, toItem: super.view, attribute: .bottom, multiplier: 1.0, constant: -30.0)
Here are the keyboard functions:
#objc func keyboardWillShow(notification: NSNotification) {
print("keyboardWillShow")
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.searchRadiusView.center.y += (-1 * keyboardSize.height)
view.constraints[18].constant += (-1 * keyboardSize.height)
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
}
#objc func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.searchRadiusView.center.y += 1 * keyboardSize.height
view.constraints[18].constant -= (-1 * keyboardSize.height)
UIView.animate(withDuration: 0.5, animations: {
self.view.layoutIfNeeded()
})
}
}
constraints[18] is the constraint I created in my viewDidLoad. When I first tap the textfield, the UIView moves up the correct amount. Then, when I type my first character, it moves up that same amount again, and is now near the top of the screen. When I dismiss it, it moves back down to the height it was just previously at (just above the height of the keyboard, which is now dismissed). The next time I edit text, it moves up again, but not the full keyboard.height amount for some reason. When I dismiss, it goes down the FULL keyboard height. This then repeats until the UIView falls off the bottom of the screen. This movement is so strange and I have no idea what the problem is. All I wanted is for it to move up and down with the keyboard. Any ideas how to fix this? thanks
You are doing too many things wrong for me to list, so I have just fixed your project and made a pull request. Merge the pull request into your repo and you will see that it now works fine.
Just for the record, here are some of the main things you were doing wrong:
You added a bottom constraint, in code, to the blue view. But you already had a bottom constraint on the blue view. Thus you now have two of them, and any change in one of them will cause a conflict. The Xcode console was telling you very clearly that this was happening, but you ignored what it told you.
You were changing the constraint constant but also changing the blue view center. That probably caused no harm but it was pointless. You cannot govern a view's position by its center if you are governing it with constraints; they are opposites.
In your show and hide methods you examined keyboardFrameBeginUserInfoKey. That's wrong. You want to examine keyboardFrameEndUserInfoKey. The question is not where the keyboard is now but where it will be when it finishes moving.
The animation is wrong. There is no need for a UIView animation; you are already in an animation block. Just call layoutIfNeeded and the animation will happen together with the movement of the keyboard.
Your entire way of speaking of and accessing constraints is wrong. You use an incorrect expression super.view (you probably meant self.view). But even more important, you attempt to access the desired constraint by saying self.constraints[2]. That sort of thing is fragile in the extreme. The correct approach is to keep a reference to the actual constraint (an instance property). In this situation, since the constraint already exists (in the storyboard), that reference can be an outlet.
So, with all that said, here's my rewrite of your code; this is the complete code needed:
class ViewController: UIViewController {
#IBOutlet weak var sampleTextField: UITextField!
#IBOutlet weak var bottomConstraint: NSLayoutConstraint!
var originalConstant: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIWindow.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIWindow.keyboardWillHideNotification, object: nil)
self.originalConstant = bottomConstraint.constant
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
sampleTextField.endEditing(true)
}
}
extension ViewController {
#objc func keyboardWillShow(notification: NSNotification) {
print("keyboardWillShow")
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.bottomConstraint.constant += keyboardSize.height + 5
self.view.layoutIfNeeded()
}
}
#objc func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.bottomConstraint.constant = self.originalConstant
self.view.layoutIfNeeded()
}
}
}
With all of that said, the code is still wrong, because you are not taking account of the very real possibility that you will get a keyboardWillShow notification when the keyboard is already showing. However, I leave that for your later investigation.
I have a UITextView at the top, a UITextView in the centre and a UITextView at the bottom.
I want to move the view up when the keyboard presents if using the bottom UITextView or the centre UITextView but when using the top UITextView the view shouldn't move.
How do I make this work?
func showLoginKeyBoard()
{
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification)
{
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
{
if self.view.frame.origin.y == 0
{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
func textViewDidBeginEditing(_ textView: UITextView)
{
if textView == centreTextView
{
showLoginKeyBoard()
}
if textView == bottomTextView
{
showLoginKeyBoard()
}
}
Currently when any of the UITextViews becomeFirstResponder the view moves up which means when using the top UITextView it isn't visible.
How can I make sure the top UITextView doesn't move the view up?
Before answer your question ,
According to your code every time user click on textView you add Observer.Don't do this. Add observers at viewDidLoad() and don’t forget to remove observers in viewDidDisappear(). Otherwise it will cause to memory leaks.
Now,Answer to question
define fileprivate optional textView
var currentTextView:UITextView?
Then assign textField in textViewDidBeginEditing
func textViewDidBeginEditing(_ textView: UITextView){
currentTextView = textView
}
now you can show or not according to currentTextView
#objc func keyboardWillShow(notification: NSNotification){
if let txtView = currentTextView{
txtView != topTextView {
//move up the view
}
}
}
I recommend using IQKeyboardManager library which handles all your keyboard events with only a single line of code
I would like to customize the scroll-offset when showing the keyboard. As you can see in the GIF, the Textfields are quite close to the keyboard and I would like to have a custom position. The "Name" textfield should have 50px more distance and the "Loan Title" textfield should just scroll to the bottom of my UIScrollView.
To be able to scroll past the keyboard I'm changing the UIScrollView insets. Strangely iOS automatically scrolls to the firstResponder textfield (see GIF).
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification) {
// get the Keyboard size
let userInfo = notification.userInfo!
let keyboardEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
// update edge insets for scrollview
self.mainScrollView.scrollIndicatorInsets.bottom = keyboardEndFrame.height - self.view.layoutMargins.bottom
self.mainScrollView.contentInset.bottom = keyboardEndFrame.height - self.view.layoutMargins.bottom
}
I already tried to use the UITextfieldDelegate method: textFieldDidBeginEditing(_ textField: UITextField)
I also tried to use the Apple way described here: https://stackoverflow.com/a/28813720/7421005
None of these ways let me customize the automatic scroll position. In fact it kind of overrides every attempt. Does anyone know a way to workaround this?
You can prevent your view controller from automatically scrolling by setting automaticallyAdjustsScrollviewInsets to false as described here.
Implementing keyboard avoidance is also pretty straight forward. You can see how to do it here.
I don't believe there is any way to keep the automatic positioning and apply your own custom offset. You could experiment with making text field contained in another larger view and making that larger view the first responder, but that would be a hack at best.
I found a solution by myself. The problem was that the automatic scroll (animation) was interfering with my scrollRectToVisible call. Putting this in async fixed the problem.
It now looks similar to this:
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification) {
// get the Keyboard size
let userInfo = notification.userInfo!
let keyboardEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
// update edge insets for scrollview
self.mainScrollView.scrollIndicatorInsets.bottom = keyboardEndFrame.height - self.view.layoutMargins.bottom
self.mainScrollView.contentInset.bottom = keyboardEndFrame.height - self.view.layoutMargins.bottom
var frame = CGRect.zero
if nameTextField.isFirstResponder {
frame = CGRect(x: nameTextField.frame.origin.x, y: nameTextField.frame.origin.y + 50, width: nameTextField.frame.size.width, height: nameTextField.frame.size.height)
}
if titleTextField.isFirstResponder {
frame = CGRect(x: titleTextField.frame.origin.x, y: titleTextField.frame.origin.y + titleShortcutsCollectionView.frame.height + 25, width: titleTextField.frame.size.width, height: titleTextField.frame.size.height)
}
DispatchQueue.main.async {
self.mainScrollView.scrollRectToVisible(frame, animated: true)
}
}
I have a view controller with a UIScrollView pinned to all 4 sides. Then a UIView inside with all its 4 sides pinned to the scroll view and as well as equal width and equal height constraints added.
Inside this view, there are two container views. These two container views embed two separate UITableViewControllers. I'm getting no auto layout errors or warnings.
This is how it looks when it's run.
In the bottom table view, one cell(middle one of the first section) has a UITextField and the bottom cell has a UITextView. So obviously when the keyboard appears, these fields get obscured.
So what I wanted to do was to move the entire view that contains both container views when the keyboard appears. That's why I embedded it inside a scrollview. I use this code to monitor keyboard showing/hiding and set the scrollview's content inset accordingly.
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func keyboardWillShow(notification: NSNotification) {
adjustInsetForKeyboard(true, notification: notification)
}
func keyboardWillHide(notification: NSNotification) {
adjustInsetForKeyboard(false, notification: notification)
}
func adjustInsetForKeyboard(show: Bool, notification: NSNotification) {
let userInfo = notification.userInfo ?? [:]
let keybaordFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let adjustmentHeight = (CGRectGetHeight(keybaordFrame)) * (show ? 1 : -1)
scrollView.contentInset.bottom += adjustmentHeight
}
}
But there are a couple of issues.
When the keyboard appears and although I change the scrollview's content inset, the entire view doesn't move. It does this weird thing. The bottom tableview goes under the top table view. It's easier to show so here is a video.
Tableviews overlapping issue
When I refocus on a textfield for more than 1 time, the scrollview goes off the screen!
Tableview going off the screen
Anyone got an idea why this is happening?
Dropbox link to demo project
A UITableViewController already automatically handles the adjustment of the content inset when the keyboard is shown. There is no documented way to disable this behaviour. You can override viewWillAppear(animated: Bool) in your StaticTableViewController and not call it's super method:
override func viewWillAppear(animated: Bool) {
}
It's probably where the UITableViewController registers for the keyboard events, as this disables the content inset adjustment. However, I can't tell you if there will be other adverse effects of not calling viewWillAppear of UITableViewController and this behaviour might change with future versions of iOS. So a safer way is to just not use UITableViewController and add a standard UITableView to a UIViewController and load your cells in there.
Also note that with your design the user could scroll all the way up and hide your lower content view behind the keyboard. Then the user can't scroll down as any scrolling only scrolls and bounces the upper tableview. So rethink your design or hide the keyboard as soon as the user scrolls
There are couple ways:
To observe UIKeyboadWillShowNotification and UIKeyboardWillHideNotification, get keyboard size data from it and adjust your scrollView contentInset bottom value properly.
func viewDidAppear() {
super.viewDidAppear()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "increaseContentInset:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "decreaseContentInset:", name: UIKeyboardWillHideNotification, object: nil)
}
func viewDidDisappear(){
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func increaseContentInset(notification: NSNotification) {
let endRect = notification.userInfo![UIKeyboardFrameEndUserInfoKey]
scrollView.contentInset = UIEdgeInsetsMake(0, 0, CGRectGetHeight(endRect), 0)
}
func decreaseContentInset(notification: NSNotification) {
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
Use the library for it. I strongly recommend you to use TPKeyboardAvoiding
I have tableView size set by AutoLayout (bottom to Bottom Layout Guide, top to another view and so on but first UISearchBar to Top Layout Guide):
Controller with tableView:
I need to change table offset when keyboard is shown so I have these two methods:
// MARK: - Keyboard
func keyboardWasShown (notification: NSNotification) {
let info: NSDictionary = notification.userInfo!
let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue
let keyboardSize: CGSize = value.CGRectValue().size
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
self.tableView.scrollIndicatorInsets = self.tableView.contentInset
}
func keyboardWillBeHidden (notification: NSNotification) {
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
}
And it's working but I have problem when keyboard is shown. The last item can't be selected and instead of that I get previous item. I tapped where is last item and it should navigate to detail page with last item but instead I see detail page with previous item. It isn't shift for all items but just for the last one and when I filtered to just one item it's working okay. When keyboard is hidden (and items are still filtered) then It's okay too (it selects the right thing). So I guess the problem must be here:
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
self.tableView.scrollIndicatorInsets = self.tableView.contentInset
So where could be problem? Thanks for help
I got my solution. I was using UIKeyboardWillHideNotification and method keyboardWillBeHidden was called before didSelectRowAtIndexPath so contentInset of tableView was set back to UIEdgeInsetsZero and then there was wrong indexPath. So now I use keyboardDidHide instead of keyboardWillBeHidden:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidHide:", name: UIKeyboardDidHideNotification, object: nil)
...
func keyboardDidHide (notification: NSNotification) {
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
}
So, assuming keyboardHeight is storing your keyboard height (pay attention because the keyboard frame may vary across devices), try this:
CGRect *frame = [tableView frame];
frame.size.height -= keyboardHeight;
[tableView setFrame:frame]
Do the same thing (but replace -= with +=) when keyboard hides.