I am creating a table view cell with a text view inside and my problem is that I do not want the text view to scroll when it becomes first responder.
I have set scrollEnabledto NO and that avoids scrolling most of the time. But when the text view is completely filled up with text (I do not allow the user to enter more text than the text view can contain without scrolling) it scrolls up a bit when it becomes first responder.
How can that be avoided? :/
Thanks a lot,
Stine
You can determine where the text view will scroll to when it becomes first responder by setting the selected range.
Setting it like this:
[textView setSelectedRange:NSMakeRange(0, 0)];
will keep the text view scrolled to the top.
Related
I have a group table view with 10 sections, each section has one cell, and each cell has a text field inside.
When user tap on a text field I scroll its parent cell to bottom, so that it will be exactly above the keyboard.
Every text field has a "Next" return key, so when user tap it the next text field (in the cell below) should be first responder.
The problem is:
Suppose user tap on a text field of cell at index 5, this will make the keyboard pop up and the tableview will scroll so this cell will be right above the keyboard.
Now user press the next button..
What I want to happen is to make the text field of cell at index 6 become the first responder.
But how do I get this text field??
If I try to get the cell with cellForRowAtIndexPath I'll get nil, because the cell is invisible..
Two solutions come to mind:
1) you don't have many cells, so don't recycle them, create them when you startup and put them in an array where you can easily get them, Then, when the tablview asks for cells, you pull them out of your array.
When you want to go to next, you simultaneously tell the tableview to scroll to such and such a location or cell, and you pull the cell out of the array, add it to your view with some offscreen frame so it cannot be seen, and make the textField the first responder (to keep the keyboard up). when the tableView asks for the cell, you MAY need to reset the frame, or both reset the frame and remove it from the view. If you find you need to do this you MAY need to make it the first responder again soon (dispatch a block to the main queue so that the text field never resigns first responder.
2) Similarly, when you want to make a cell's textField the first responder, and its contained in the visible cells, then you can just scroll and make it the first responder. If its not in the visibleCells, create a cell, add it to the view as above, do the first responder stuff as above, and when you are asked for THAT cell's index, supply that cell, possibly resetting the frame.
I've used offscreen textFields to play tricks with the keyboard (to keep it up, to get it up, etc - so I know that works). What's less certain to me are the tricks with the textField being in the view or not.
This makes me think of a 3rd way. You put a UITextField offscreen in the view. When you want to switch, and the cell is not yet visible (that is, created), you tell the offscreen textField to be firstReponder, then track the tableView scrolling, and when it ends the textField you WANT s first responder is now visible, so you can transfer (ie set) firstResponder on it, keeping the keyboard up.
I do not know how you create your rows but you need to consider, that UITableView recycles the cells. That means that the text field could be theoretically the same as before, just the content changes.
So all you need to do is make sure you know the textfield that is positioned right above the keyboard. I would not be surprised if this will be created when the view enters and never actually changes.
Again, the table recycles the cells and their content holders only filling in the corresponding data for the row as you provide it.
If you post your code that creates and recycles the cells I could be more specific.
I have 2 cells in a group in a tableview, the first has a textview in it, (2 lines max). When the user selects the textview, the keyboard appears and moves the textview up from under the keyboard. the second cell only appears when the textview has focus.
However the second cell is still hidden by the keyboard, I have tried to the various methods that scroll the tableview, but the result is always jerky, with the table moving up and down rapidly. Probably because its inserting a row, scrolling up for the keyboard and me trying to make it scroll up even more all at once.
How can I prevent the keyboard from moving scrolling the table view at all, so that I can do the scrolling myself exactly how I want it, and avoid the ugly fight between the two methods.
Did you try this method by setting scroll position to UITableViewScrollPositionTop ,if this not work ,just make your second cell that with textfield :)
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
Use a UITableViewController instead a UIViewController (for the ViewController where your UITableView is placed). That will solve the scrolling issue.
If not possible, you have to scroll by your self (pain in the ass).
See: Making a UITableView scroll when text field is selected
I have a grouped UITableViewController with one section and many rows. Each cell consists of two elements: a UILabel with a description and a UITextField for an input. A form, so to speak ;-)
Now I have a problem when I try to scroll the entire UITableViewController. Only on about half of my finger movements, it scrolls.
After I've studied the phenomenon more accurately, I noticed that the scroll works only if I hit the UILabel with my finger. If I hit the UITextField coincidentally, the UITableViewController does not scroll.
What can I do to solve the problem?
One thing you could try is to create the UITextFields with userInteractionEnabled = NO, then when the cell is selected, set userInteractionEnabled and call [textField becomeFirstResponder] on it.
Although, I have just the same kind of tables and haven't noticed any scrolling problems...and I didn't set userInteractionEnabled = NO. Have you perhaps set the cells' selection style to none? In my tables, when I tap on the row, I get a row selection. Swiping, even touching down over the text field, scrolls. But in edit mode, where I disallow row selection, if I touch down over the text field and delay just a little before swiping, I don't get a scroll, and sometimes the text field becomes first responder.
Hopefully that will give some things to think about or experiments to try.
Harrr pirates!
I'm trying to make a data entry screen using a UITableViewController. It contains a grouped table view with two sections, each with a few (2 and 4) rows with a UITextField inside them.
When any of the UITextFields become first responder they scroll into view automatically. Great, you would say, but I want to scroll the whole section into view, not just the row containing the UITextField that became first responder.
I don't allow the user to scroll the UITableView by hand and I know to what position it should scroll to display correctly. However, I can't get my UITableViewController to stop automatically scrolling the UITextField into view for me.
Searching StackOverflow I can find a lot of questions about how to resize the UITableView when the keyboard appears, but this doesn't stop the automatic scrolling.
Any suggestions? Your help would be much appreciated!
TableView is basically an extension of scrollView therefore if you want to do the scroll yourself you should use the scrollview API.
Set the contectSize property to the size of the screen without the keyboard and the contentOffset to the location on the table you want to scroll to
The "too long; didn't read" version: Is there any way to disable the automatic scrolling behaviour of UIScrollView when telling a UITextField to becomeFirstResponder?
I have a scroll view with paging enabled and several views as subviews, each subview being controlled by its own view controller. Each subview has a UITextField.
The requirement is that when a page is scrolled into view, it's text field should become first responder.
This is fine when using finger swipes to scroll -- I use the scroll view delegate method scrollViewDidEndDecelerating: to know when scrolling stops and a page is in view, I can tell the text field to become first responder.
However, when the scroll view is "autoscrolled", as in when telling the scrollview to scrollRectToVisible:animated:, the scroll view delegate method for deceleration isn't called. I use this method when scrolling newly created pages into view without the user's interation, or when the user taps the UIPageControl.
My solution was to simply set the first responder status of the text field before telling it to scroll into view - but it seems that telling a text field that is in a scroll view to become first responder causes the scroll view to automatically scroll it into view.
I assume this is behaviour used when putting text fields in table view cells (since table views are scroll view subclasses). If you set up a small test app, with a table view, and a text field within a table cell, if the keyboard would obscure the table view cell when it becomes first responder, the table view will automatically scroll it to be visible.
I don't understand, though, why this behaviour occurs in my example, where I'm not using a table view - just a plain scroll view.
I should also mention that my scroll view has vertical scrolling disabled and only scrolls horizontally.
I have tested in another test app that puts text fields as direct subviews of a scrollview (no view controllers or container views) and the same happens. If you tell a text field that is offscreen to become first responder, the scroll view with automatically scroll it for you.
This wouldn't normally be a problem, but it seems to screw up the paging of the scroll view. When I scroll with my finger, each view bounces and is centred properly. But when I scroll a rect to be visible with animation and tell a text field to become first responder, scroll view seems to become conflicted with itself and the view is only scrolled part of the way into view, and isn't centred.
Then, if I touch a view using my finger (not swipe, or even move), the scroll view jumps back to the first page.
My current work around for all this silly auto scrolling behaviour is to use an NSTimer to determine when to update the first responder.
I do the manual scrolling in code using scrollRectToVisible:animated and then after 0.3 seconds, call my method to update the text field to be first responder. (0.3 seconds was trial and error, trying to see which seemed to be the smallest amount of time to allow for the animation but still be long enough not to cause the conflict with the scrollview.
As you can see, this isn't elegant, and is likely to break.
Is there any way to disable the automatic scrolling behaviour of UIScrollView when telling a UITextField to becomeFirstResponder?
Call becomeFirstResponder, then right away, set the contentOffset of the scrollview to its current position..
[textField becomeFirstResponder];
[scrollview setContentOffset:scrollview.contentOffset animated:NO];
Not an answer to your question, but it should fix the problem:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
// Make the text field first responder...
}