I have a UITableView with a few UITableViewCells. Each cell contains a UITextfield. Tapping the textFields causes the keyboard to display. Unfortunately the keyboard covers some of the cells that are lower on the iPhone's screen.
What's the easiest way to scroll the tableView up when the keyboard is displayed?
I've experimented with the contentOffset. This works for actually scrolling the tableView. However I'm having trouble determining which cell is "active" (has a textField that's currently the firstResponder). My app supports both portrait and landscape orientation so the tableView sometimes needs to be scrolled when the device rotates.
A simple way to keep track of which textField is currently active is to set its tag property to the indexPath's row on cellForRowAtIndexPath:
Another way is to use this line:
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[textField convertPoint:textField.frame.origin toView:self.tableView]];
on textFieldShouldBeginEditing: for when the textField becomes active, or on your rotation callbacks for when the device rotates, and then scroll the tableView using scrollToRowAtIndexPath:atScrollPosition:animated:
I am writing an app that has a similar issue. The way that I am handling keeping track of the first responder, or active UITextField, is by using a callback. I call a method using the UIControlEvent UIControlEventEditingDidBegin.
Where I setup the UITextField:
[self.nameUnitNumberField addTarget:self.textFieldDelegate action:#selector(handleEditingDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
Then in the delegate class, I set the current text field:
- (void)handleEditingDidBegin:(id)sender
{
self.currentNameField = (UITextField *)sender;
}
This seems to be working well. It also avoids the use of tags. I've heard that there are potential problems with it and it's best to avoid it when not needed.
Once you have the active UITextField, you can use either the contentInset and scrollToRowAtIndex: as described here: Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard
or just resize the table view while the keyboard is on screen. That would be simplest way to do it.
Related
I have a UICollectionView with some UICollectionViewCells that contain UITextFields. When a UITextField is selected for editing, the keyboard shows and the UICollectionView moves up accordigly to make the active UITextField visible. I am relying on default behaviour with this and the UITextField is actually appropriately positioned when UIKeyboardDidShowNotification is launched. However, right after that, the UICollectionView will scroll automatically back to the top, and the UITextField will move out of view (behind keyboard).
What could be causing this and how could I debug this behaviour? Please note, that any custom code for UIKeyboardDidShowNotification like scrollRectToVisible or Apple's sample code will not help, as the scrolling off viewable areas happens at a later stage overriding that code. I am just unable to pinpoint the cause of this.
EDITED to add:
An animation to show how the view behaves with a hard keyboard (iPad Pro) when clicking Next to cycles fields. This works fine.
An animation to show how the view behaves with a soft keyboard when clicking Next to cycles fields. Here the keyboard avoiding does not work.
I initially load a hidden tableView with some data and show this when I press a button, simulating a drop down menu.
In the same view, I have a UIButton which when pressed, programmatically creates another UIButton and a UITextField.
The problem is that when I load again, the tableView and all the textFields and buttons are superposed.
This is what happens:
My question is what can I do for keep the tableView in front of in the view.
When you do -addSubview:, the view is added as the top-most view.
Which is the reason why your newly added textFields are appearing above your tableView.
The quickest way to solve your issue would be to make the tableView the top-most view when you're planning to show it.
Example:
[yourTableView setHidden:NO];
[self.view bringSubviewToFront:yourTableView];
Assuming that self.view contains yourTableView
What you are experiencing is a cell reusability issue.
If you are adding controls on cells, when they get reused They will keep the textFields which gives you this strange juxtaposition result.
You have to take proper care of removing the textField which makes things more complex.
Solution is to subclass UITableViewCell so that you have prototype cell with textfield and another one without.
I have a UItextView inside a UITableViewCell. When I select some text inside the textview, I have a strange animation: the cell or the textview (I don't know wich scrollview is really moving, may be both) start scrolling up and down maybe because both scrollviews (Tableviewcontroller's scrollview or UItextView's scroll view ) want to focus the selected text for their own in different ways. I've tried anything but this bad animation is still here.
The Tableviewcontroller and the textfield was made with storyboard.
ps: sorry for bad english
Use
[tableView setScrollEnabled:NO];
when the textView is focused.
if you want to show a static text just use the uilabel, which is enough.
but if you want to show and let the user edit the textview, you may allow to edit it while the tableview is in edit mode.
I have a UITableView in which I have several custom cells and my last cell contains a UISearchbar in it. I have wired up the appropriate delegates and referencing outlets and can get the keyboard to show up.
The problem I am facing is that the searchbar is at the very bottom of my tableview i.e. it is part of the very last cell. So even though the keyboard gets shown, the searchbar is hidden below it as I suspect it is unable to scroll to that location since its the last cell in the view.
Does anyone have any thoughts on this and how to overcome this situation.
The easiest solution is to have your view controller be a subclass of UITableViewController. This type of view controller will handle issues exactly like these.
If you cannot, you should listen to keyboard will show/hide notifications and set contentInset and scrollviewIndicatorInsets bottom of the table view to the keyboard height. Then you can scroll the specific cell into visible. When the keyboard hides, set the bottom to the previous value.
Yes....Two ways
1) either you can change the frame of whole tableView and pull the whole table up by decreasing the y position of tableView
[tableView setFrame:CGRectMake(100,100,100,100)];
or
2) You can change the contentOffset of the table to programatically scroll the tableView's last cell so that it is visible to the user.
[tableView setContentOffset:CGPointMake(0,400) animated:YES]
Hope this will help you.
Everyone wants to move the UITableView when the keyboard pops up, but I'm looking for a way to disable the automatic animation to the cursor when the keyboard pops up. I'm experiencing an odd jerking / jolting / erratic scrolling behavior when the keyboard pops up and causes the UITableView to scroll to the cursor (to avoid blocking it).
Each of my UITableView cells has a UITextView in it. I don't commit any other animations when the keyboard pops up.
At this point, I would like to disable the animation completely and manually scroll to a desired CGPoint.
Thank you!
The automatic scrolling code resides in tableViewController, so auto-scrolling can't be disabled. Instead of subclassing from UITableViewController you can subclass from UIViewController and use a tableView inside it.
If you are willing to use UITableViewController itself, you can override viewWillAppear and don't call [super viewWillAppear].