iOS - Disable Scrolling when Editing Text Field - iphone

I am trying to disable the scrolling (and preferably all background touches) when editing a uitextfiled.
Here is the scenario:
I have a ScrollView setup with several text fields. Upon editing a text field I animate the view up/down so that the text field will sit just above where the keyboard/keypad will appear. In the case where I changed the keyboard style to phone/number pad I have a subview setup with a done button available to dismiss the keyboard when editing is done. All of the animation works as expected. However, the user is able to scroll the background and select other fields. I would like to disable this functionality so that all they can do is view what is shown on the screen and edit the one field.
I've tried doing the [scrollView setUserInteractionEnabled:NO] which locks the scroll view in place, however the keyboard is not available to type information into the text field. Again in the cases where the keyboard is set to phone/number pad my subview appears, just no keyboard.
I have also tried setting the textfield being edited as the first responder but that seems to mess up the view entirely, probably due to my animation of the view subview.
Is there a way for me to still animate the view the way I want and disable the scrolling of the scroll view, or all background touches in that view entirely?

Make your viewController the delegate of the textField and in the - (void)textFieldDidBeginEditing:(UITextField *)textField disable the scrolling.. self.scrollView.scrollEnabled = NO; and in the - (void)textFieldDidEndEditing:(UITextField *)textField set the scrolling enabled again...
You can also disable all the other textfields as your requirement states.
self.otherTextField.userInteractionEnabled = NO;
Hoping this helps.

Try this
- (void)textFieldDidBeginEditing:(UITextField *)textField{
scrollView.scrollEnabled = NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
scrollView.scrollEnabled = YES;
}

Did you try:
scrollView.scrollEnabled = NO
?

Related

Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO

I have a UITextField that is first responder. I want to show keyboard when entering the view but I want to do that the user will not be able to edit it and the cursor will be hidden all time as well.
When you click on a keyboard letter, it will be written in the UITextField, but the user will not be able to edit nothing there, even not to copy.
Thanks!
Ok, per my comment, my solution is to have a surrogate UITextField that has its hidden property set to YES. What I do is add that hidden text field to the view, and call becomeFirstResponder on it. The user has no idea this text field exists. In the delegate callback from the text field, I take the text the user typed in and add it to a UITextView (though you could add the text to whatever you wanted, like a UITextField like in your question). I turn off userInteractionEnabled for the visible text view. This creates the effect you desire.
I created a sample project that I uploaded to Github. (If you aren't familiar with it, just click the zip button to download it, unzip it, and open the .xcodeproj file). https://github.com/MaxGabriel/HiddenTextField
I had a UISearchBar property in my viewController. And I did it like this:
- (void)viewWillAppear:(BOOL)animated
{
[self.searchBar becomeFirstResponder];
}
This should work the same for a UITextField.
As for disabling editing, use:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
You should have set your viewController to be the delegate of UITextField.
Edited answer: Try this:
1. [txtField becomeFirstResponder];
2. txtField.enabled = NO;
3. when some press on keyboard, then txtField.enabled = YES;
Check this out : http://www.youtube.com/watch?v=cKV5csbueHA

Hide keyboard when we scroll with TableView after we searched

I want to hide the keyboard when I scroll on TableView. That means, I search for an item and I see the results, when I scroll on the TableView the keyboard will hide or dismiss. Look at this image.
You need to track the action that runs when the tableView is being scrolled. UITableView is the subclass of UIScrollView. So, you can use all the methods from UIScrollViewDelegate for your table.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[textfield resignFirstResponder];
}
This will work.
Set up the scrollview delegate in your header then make it so when
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollview{
[self.textfield resignFirstResponder];}
and when he stops scrolling
- (void)scrollViewDidEndDragging:(UIScrollView*)scrollview willDecelerate:YES{
//open keyboard back up}
edit:
my bad, i confused scrollview and tableview. disregard anything up there, i cant find anything for your problem

show subview when UITextField touched

I'm pretty new to iPhone development, so please excuse my ignorance. I've googled this a bit and so far come up with nothing useful. Here is what I would like to do:
I would like a subview to popup(with the rest of the screen showing in the background) when a UITextField is touched. The popup is a calculator UIView that I created in the IB. It seems it is better to have a popup show than a customized keyboard, due to Apple's developer guidelines.
Here is my question. How do I capture the touch on the UITextField and how do I show the subview?
I have tried things like below to show the subview, with no luck:
CustomCalculator *customCalc = [[CustomCalculator alloc] initWithNibName:#"CustomCalculator" bundle:nil];
UIViewController *calcController = [self.customCalc.view];
[self.view addSubview:calcController.view];
Use the delegate method:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
//Add your subview here
return NO; //this will stop the keyboard from poping up
}
This way when someone taps the textfield, your view will popup instead of the keyboard.
Now once the user is interacting with your view, you will have to manipulate the string in the textfield.text property directly as a reaction to the User tapping buttons in your view.
Implement the UITextFieldDelegate and the method for it is
-(void) textFieldDidBeginEditing:(UITextField *)textField
The above method is fired when you touch the UITextField. You may then position the UIPopoverController (I'm guessing that is what you're using to show the view in a popup) and as soon as you're done there pass the values back to the UITextField. Hence the popover's/viewcontroller presented's delegate should be your textfield object.
EDIT: After seeing the other answer below it struck me that I forgot to tell you how to stop the keyboard from showing. Just make this the first line in the method I've mentioned above:
[textField resignFirstResponder];

how to check if a view is behind the keyboard on iphone

in my application i have a bunch of textlabels and textviews. Sometimes the textview is underneath the keyboard. My question is if there's a way to check if a textview is behind the keyboard to move it up. I already know how to move views up, and i know about the keyboardWillAppear notifications, but i don't know how to check if the view is behind the keyboard. The thing is that i don't want to move the textview if it's not underneath the kayboard. How can achieve that?
Thanks in advance.
I would do the check for first responder as shown above
[text isFirstResponder];
then I'd check to see if the bounds of the text field's bounds are less than 215 (because i think that's the max height of the keyboard) and accommodate from there. so all together it looks like:
if([text isFirstResponder]){
if(text.bounds.y > 215){
text.bounds.y = CGPointMake(text.bounds.y-(text.bounds.y-215));
}
}
I think the only way to see this is to verify each UITextField and UITextView if it returns YES for
[_text isFirstResponder];
If any UITextField or UITextView is First Responder, than it means that the keyboard is on the bottom of the screen.
You can see the keyboard will appear by listening to UITextFieldDelegate and UITextViewDelegate ShouldBeginEditing events:
for UITextField it is:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;// return NO to disallow editing.
and for UITextView it is:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
Hope it helps.

How to hide the iPhone keyboard? resignFirstResponder does not work

I have the following code...
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//some code here...
NSInteger theTag = textField.tag; //I set the tag to 5 in IB
if (theTag == 5) {
//self.showDatePicker;
[textField resignFirstResponder];
}
}
The problem is, the keyboard never disappears. Another thing to note is that I have some other methods that move the view up and down based on the position of the textfield selected. Maybe that's messing up my Responder, but I just don't understand why the keyboard won't go away.
Also, I might just be doing this all wrong. I want this textField, when pressed, to hide the keyboard and show a date picker. Should this be in a different method?
If you're targeting iOS 3.2+, I'd suggest looking into UITextField's inputView property. You can assign a custom view (i.e. a data picker) to be displayed when the text field becomes the first responder instead of the keyboard.