Trigger done button touch in iPhone - iphone

How can i trigger done button touch in iPhone?
I have an asynchronous task which pops me small tableView (adds it to subView). When one of my textFields is focused - keyboard is shown and overlays my tableView.
I tried to call resignFirstResponder on focused textField in "textFieldShouldBeginEditing" and it didn't help. So i think maybe explicit trigger of done button will work.

You can animate the entire frame of the view whenever the textfield is focused, so that it slides up and display the table and also the textfield without any hindrance.

you could have the done button as right navigation button of your view controller.
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(doneButtonPressed:)];
self.navigationItem.rightBarButtonItem = doneButton ;
[doneButton release];
Implement doneButtonPressed: method as below.
-(void) doneButtonPressed:(id) sender
{
[myTextField resignFirstResponder];
}

If you don't want the text field to becomeFirstResponder or to be edited, return NO from textFieldShouldBeginEditing method.

What I understood from your question that your other fields get hided when you enter in the textfield.For this you need to animate the view.I am going to put the code for this for you.
In your header file declare this.
CGFloat animatedDistance;
And in your .m file put this code and you won't face any problem.Your view will automatically scroll up and down when you start editing the text fields so making the space to view your fields properly..
#pragma mark -
#pragma mark (Text Field Delegate Method)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
CGRect textFieldRect;
CGRect viewRect;
textFieldRect =[addProScrollView.window convertRect:textField.bounds fromView:textField];
viewRect =[addProScrollView.window convertRect:addProScrollView.bounds fromView:addProScrollView];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame;
viewFrame= addProScrollView.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[addProScrollView setFrame:viewFrame];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.tag==0)
{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
CGRect viewFrame;
viewFrame= addProScrollView.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[addProScrollView setFrame:viewFrame];
[UIView commitAnimations];
}
}
Thats it .You are now done just run your code.
Cheers......

Related

How to move the focussed text field to the middle of the view after keyboard popups

In my iPad im creating SplitView application. I have a UITableVewtable with multiple rows, each row/cell having three UITextField
When i tap on UITextField some fields are hide by the keyboard.
So im using TPKeyboardAvoidingScrollView framework but it does not works on ios 5.0+.
some times unable to scroll the scroll view on the table.
So I want to move the focussed cell to the middle/ just above the key board at every time
What should I use?
Thanks in Advance.
Try this code. Put this in your .h file :
CGFloat animatedDistance;
In your .m file :
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 264;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 352;
Add this static const before your #interface myclass () You can change the keyboard height values above as per your need. And then add the following code :
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size. height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
what you want is to animate your view such as when you touch the textField it should go up and when you end typing it should go down. You can achieve this by implementing two UITextFiled delegate methods and some little animation as follows :
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self startAnimatingTextField: textField up: NO];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self startAnimatingTextField: textField up: YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.1];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[UIView commitAnimations];
}
-(void)startAnimatingTextField:(UITextField *) textField up: (BOOL) up
{
const int distance = 120; /* modify conform your needs */
const float duration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
Hope this helps !
First open the xib file and set the Content insets(In Scrollview Size) to 200
Now update following two methods as below in .m file.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cell=#"Cell";
Cell *customCell= (Cell *)[tableView dequeueReusableCellWithIdentifier:cell];
if (customCell==nil) {
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:self options:nil];
for (id object in bundle) {
if ([object isKindOfClass:[Cell class]])
{
customCell = (Cell *)object;
break;
}
}
}
customCell.IBTxtfield.tag=indexPath.row;
return customCell;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSIndexPath *path=[NSIndexPath indexPathForRow:textField.tag inSection:0];
[IBtblView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

How to show keyboard upper side of UItextfield when we enter text, in iphone?

I have a UITextfield in which I am entering value. I don't want to scroll myself or adjust keyboard myself , I want Keyboard automatically adjust itself when I enter text in UITextfield and always show below the textfield.
Any hints from experts would be very welcome.
Here is some code that I have used. Basically, what we're going to do is animate the position of the view whenever a UITextField gets focus. To do this, we must make our UIViewController a delegate of our UITextFields
define a variable float animatedDistance in .h of the file
#interface <ClassName>
float animatedDistance;
#end
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
Now that the view has been 'pushed up' when a UITextField is selected, we want to make sure that we slide it back down when we're done:
- (void) textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
For more Visit Apple Documentation
Animate the frame (viewcontroller.view.frame.origin.y - 215) of your view that contains the textfield when the textfield starts editing and back down when it is finished.
Use the UITextfieldDelegate methods
You can use this class which will help you to do the needful. All you need to do is make the class of that UITableView to TPKeyboardAvoidingTableView. Same for UIScrollView and UICollectionView. You can used these classes to avoid the keyboard.
Hope this helps. And if it does, please accept. ;)
Thanks and Cheers.

View resizes to full screen after app becomes active

My situation:
I have a text input field at the bottom of my view (the outermost view does not scroll). When someone focuses the input, I shrink the view to accomodate the keyboard (specifically, I subtract the height of the keyboard and animate the view to a new height).
My problem:
Whenever something takes focus away from my ViewController and returns it soon after (e.g. a push notification or someone hitting the home button and coming back to the app later), the view seems to automatically resize so that the text input is back at the bottom of the screen, but the keyboard remains showing.
What I want:
I want the view to maintain the size it had before the app regained focus. The keyboard should remain showing and the text input show display above it, not move back down to the bottom of the screen behind the keyboard.
Does anyone know what I might be doing wrong or what I need to do to get this behavior?
try this code....
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

How to Drag my view a little bit up when keyboard comes up?

In my app i have some textfields which gets filled through the keyboard. There are 6 of them and the last two are in very bottom of the screen and it gets hidden whenever keyboard comes up.
Any suggestions??
Make the textFields delegate as self and add the following code to the textFieldDidBeginEditing and textFieldDidEndEditing.
Define the following constants: (the constants have been set for iPad. Tweak them for iphone)
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 302;
Add two variables in the .h file of your class. CGFloat animatedDistance; and UITextField *currentField;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentField = textField;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
CGRect textFieldRect =[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
if (orientation == UIInterfaceOrientationLandscapeLeft )
{
textFieldRect.origin.y = textFieldRect.origin.x;
textFieldRect.size.height = textFieldRect.size.width;
viewRect.size.height = viewRect.size.width;
}
else if (orientation == UIInterfaceOrientationLandscapeRight )
{
textFieldRect.origin.y =viewRect.size.width - textFieldRect.origin.x;
textFieldRect.size.height = textFieldRect.size.width;
viewRect.size.height = viewRect.size.width;
}
else if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
textFieldRect.origin.y = viewRect.size.height - textFieldRect.origin.y;
}
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
animatedDistance = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
currentField = nil;
}
Use a UIScrollview so the user can scroll down. You can make use of the inputAccessoryView of a UITextField to show buttons like next and previous, on click let the input jump to the next textfield and scroll to it.
Have a look at this question and answers at SO, same problem, different solution.

how UITextField should come up in UIScrollView?

I have a UIScrollView, added as
[self.view addSubview:myScrollView];
There are 8 to 10 UITextFields, so when I click textFields for writing something, they Keyboard comes on them, and they became back to keyboard, now what should I do so that they will look after keyboard end ???
If someone is not clear, may ask again ....
This is how your .h file should look like
#interface LoginScreen : UIViewController <UITextFieldDelegate>
{
...
UITextField *textView;
CGFloat animatedDistance;
}
//put this in your viewcontroller.m file
-(void)textFieldDidBeginEditing:(UITextField *)textField{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
CGRect textFieldRect =
[self.view convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)txtObject {
[txtObject resignFirstResponder];
return YES;
}
Pls go through these
How to make a UITextField move up when keyboard is present?
Keyboard in the way of textfields how to move view up
iPhone Keyboard Covers UITextField
How programmatically move a UIScrollView to focus in a control above keyboard?