Unwanted automatic scrolling with UIScrollView and UITextFields as subviews - iphone

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...
}

Related

Prevent keyboard from moving textview in UITableView

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

Canceling the scrolling animation when keyboard pops up on iphone

I have a UIViewController that contains UITextField. I set the inputAccessory of the textfield to a UIToolbar that I create.
My ViewController's view is inside a UIScrollView (To handle the events of keyboard popping up)
Before:
Now, when the keyboard pops up, suddenly my ScrollView is setting it's contentOffset.y to 64 with animation, so part of my view is pushed above the top of the screen:
After:
This is done by the framework because I'm using a toolbar for inputAccessoryView.
If i'm dropping the toolbar there is no animation and no content offset.
Is anyone knows how can I disable this automatic animation and scrolling?
Thank you!
You can use a combination of boolean flags and your UIScrollViewDelegate to prevent the scrolling from taking place. Scroll view delegates can be used to detect when scrolling is taking place, modify the type of scrolling, and also simply prevent it from happening, so your best approach is probably to look into detecting when the keyboard goes up (via a notification or text field delegate, whichever is more appropriate) and using that in combination with your scroll view delegate.

UITableViewCell with a UITextView inside - scrolls even when disabled!

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.

Manually scrolling UITableViewController when a UITextField becomes first responder

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

How to display scrollbar in UITableView

I want to display some kind of indication to guide user to scroll.
Usually when we touch the UITableView scrollbar appears if needed. But I want this scrollbar indication already displayed on my tableview.
How is it possible to do so?
If you have a table view that goes offscreen, you can call
[self.tableView flashScrollIndicators];
and they will flash to show the user that they are there. This is usually put in viewDidAppear.
(If you inherit from UITableViewController then you will have a self.tableView instance variable, if not then substitute another UITableView.)
If you a scroll view's entire contents fit within its view then no scroll bars are displayed; to test this display a table view with only one cell. If the content size is larger than the view's frame then scroll bars will be displayed; only then will [self.tableView flashScrollIndicators]; actually flash scroll indicators.
There's no way to force the scrollbar to appear, short of messing with the internals of UITableView(which you shouldn't do), or redesigning your own table view class.
Per the documentation of UIScrollView's showsVerticalScrollIndicator property: "The indicator is visible while tracking is underway and fades out after tracking."