I have a UITextView containing a document. If the user touches the document, the insertion point (selectedRange property) is set appropriately and the UITextView becomes the first responder (keyboard appears). YAY!
How can I do the same thing programmatically? Let's say I have a button titled "Edit at character 1,000". I want that to set selectedRange to [1000, 0] and then make the textview become the first responder.
Problems...
textview.selectedRange setter only seems to have an effect when called from viewDidAppear.
[textview becomeFirstResponder] sets the insertion point to the end of the document.
So, the best I can do is first becomeFirstResponder and then set selectedRange. The user sees the view scroll to the bottom of the document and then back up to the desired insertion point. Kinda ugly.
Should I try to hide the ugliness by hacking 'scrollEnabled' and 'editable' flags during the transition? Or is there a better way to do this?
You may be stuck doing a hack. How about this one:
Hide the view when you set the selected range (perhaps by putting another view of the same size over the UITextView) to hide the ugly scrolling. Then when the range is selected set unhide the view, some time after viewDidAppear.
Related
I have a modal window that's used for searching data from a remote server- it has a UITextField as the titleControl of the navbar for the window, and a tableview filling the window (that displays the results obviously). Now what I want to do is when the user scrolls the tableview, immediately have the textfield lose focus (resign first responder) so that the keyboard dismisses and the user has more room to scroll through the tableview (it stretches down to fill the gap left by the keyboard). Basically the same functionality as when using a UISearchDisplayController (or whatever it's called).
So I have this code for detecting the scroll event of the tableview:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[searchField resignFirstResponder];
}
Which works fine. However, the issue is that once the user scrolls the table and the textfield loses focus, you can't give focus back to it by tapping on it again. So basically once I call that [resignFirstResponser] I can never again bring the keyboard back up and edit the textfield value. Anyone have any idea why? Do I need to explicitly call [becomeFirstResponder] on the field somewhere? Because I thought that was handled automatically when the field is tapped?
Also of note- I am calling [becomeFirstResponder] on the text field right when the modal window is first called up, so the field is pre-focused. Could that have anything to do with it?
I can post more code if anyone would like, but I don't think I'm doing anything out of the ordinary with the textfield.
Thanks for any help!
You are calling the resignFirstResponder from a function which will be called everytime you scroll the UIScrollview. Hence it does not appear. You need to call resign when the uitextview goes out of focus.
You can do the following. Its a hack:
Whenever you focus on the UITextField create a invisible button to overlay your scroll view.
Capture the button press event and resign first responder
Whenever the uitextfield becomes first responder create the button
This way you will remove the bug, viz calling the method in scrollViewWillBeginDragging.
Other option would be to overrite viewDidAppear method for the uiTextField.
Or you could put your textfield into a different container and handle scrollViewWillBeginDragging by checking which scrollview sent the message.
Did u set a delegate for you searchField? I had the same issue. I popup a model view, and set the text field to be the first responder inside viewDidLoad. Everything works well for the first time. But once I dismiss the modal view controller, and reopen it. my text field cannot be focused anymore.
I found it has something to do with methods of UITextFieldDelegate. Once I remove implementation for methods
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
everything works well. but don't know why
Are you doing anything with "textFieldShouldEndEditing", like #fengd?
A problem that I had was that I was viewing a modal view, and my "textFieldShouldEndEditing" routine was incorrectly returning "NO" on a specific text field. When my modal got dismissed, I would be unable to tap on any other text-field, presumably because the old text field was still "first responder". Since it can never end editing, it fouls up all other text fields that come after it.
I realize this is 2 yrs after the fact, but maybe someone else might find this useful.
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.
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.
I have a nice clean UI within a table view which has a few text fields for the user to fill out. One of the fields is for the user's birthday.
I'd like to have it so that when the user selects the birthday field, a view containing a UIDatePicker would come up as, just like the different keyboards do when selecting a text field.
Can this be done? I would have to prevent the text field from being the first responder (to avoid having the keyboard come up) and I would have to animate the view sliding up if no keyboard was showing before.
Would presenting the view modally be an option? If so how would I go about doing it? From the documentation it seems that modal views still take up the whole screen, I just want to use the lower 216 pixels (height of the keyboard and UIDatePicker).
Any one have any tips on how to go about doing this?
Old question but the correct way to do this these days would be to set the UITextField's inputView to a picker you created somewhere. Something like this:
UIPickerView *myPicker = [[UIPickerView alloc] init];
// set picker frame, options, etc...
// N.B. origin for the picker's frame should be 0,0
[myTextField setInputView:myPicker];
When you go to edit a UITextField, iOS really just displays whatever view is at textField.inputView which by default is the keyboard, you can make it anything you want as long as it's a subclass of UIView.
Regarding animation, take a look at DateCell sample application -
http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html
And in any case, the proper way to do this is set UITextField's inputView to show the picker instead of the keyboard. That's what it's meant to do. More on that here:
How can I present a picker view just like the keyboard does?
Cheers,
Oded.
I would implement this by just animating a view containing the UIDatePicker, a Done, and Cancel button) up from the bottom of the screen. Using CoreAnimation, this should be pretty easy.
Why are you using a text field if you don't want to accept user input from a keyboard? Instead use a UILabel subclass (where you override the touchesBegan/Ended:withEvent: set of methods to show the UIDatePicker) or a UIButton (where your action is a method which slides up the UIDatePicker).
How can I programmatically assign focus to a specific UITextField in a view? I have several fields and on view display, I'd like to put the cursor on a specific field, not the first one at top.
Try doing this in viewWillAppear:
[desiredField becomeFirstResponder];
By making the field the first responder, it have focus and the keyboard will be displayed.
Set the first responder for your view to be the text field. This can be done in IB.
In swift:
desiredField.becomeFirstResponder()