i want to increase the size of UITextField when editing begins, but just cant get the code to do it.
Gomathi is correct. Just to add to his answer if you want the textFIeld to expand when editing begins and shrink back again when editing ends and also want to animate it, you might wanna do something like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView animateWithDuration:0.5 animations:^{
textField.frame = CGRectMake(textField.frame.origin.x-20, textField.frame.origin.y, textField.frame.size.width+40, textField.frame.size.height);
}];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[UIView animateWithDuration:0.5 animations:^{
textField.frame = CGRectMake(textField.frame.origin.x+20, textField.frame.origin.y, textField.frame.size.width-40, textField.frame.size.height);
}];
[textField resignFirstResponder];
return YES;
}
Implement UITextFieldDelegate.
In - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField, set the new frame size to the textfield. Then call, [textField setNeedsDisplay];.
Related
in my view i have 14 text fields in a scrollview.
To move the view up /down while keyboard appears/ disappears i have set the scroll view frame sizes
#pragma mark - Text field view delegate methods
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
//To move the scroll view up to avoid keybord covers the text field
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
//scrollView.frame = CGRectMake(scrollView.frame.origin.x, (scrollView.frame.origin.y - 45), scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView setContentSize:CGSizeMake(200, 1100)];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
//To move the view down
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
[scrollView setContentSize:CGSizeMake(200, 900)];
//scrollView.frame = CGRectMake(scrollView.frame.origin.x, (scrollView.frame.origin.y + 45), scrollView.frame.size.width, scrollView.frame.size.height);
[UIView commitAnimations];
}
for return key board focussed to the next field and for the last field keyboard resign
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if(textField == nameField) {
[emailText becomeFirstResponder];
} else if(textField == emailText) {
[mobileText becomeFirstResponder];
}
else if(textField == mobileText) {
[iPhoneText becomeFirstResponder];
}
else if(textField == iPhoneText.value) {
[companyText becomeFirstResponder];
}
else if(textField == companyText) {
[roleText becomeFirstResponder];
}
...
......
...........
else if(textField == lastfield) { // Last fiels
[textField resignFirstResponder];
}
return YES;
}
But my intension is when ever a text field is focussed that text field is moving up to the keyboard top or center of the view
How to do it
I use TPKeyboardAvoiding for my apps, it seems to work perfectly for me, perhaps give it a try.
I have set up some text fields and labels to look like a simple form which will have to get some user input. My problem is however that whenever some of the text fields are selected and the keyboard slides out, the text fields are covered making it impossible to see the input. My question is how can I move up the text fields so that they stay in view when selected. Maybe someone who is more experienced can help me on this.
A good solution for this which I like to do is listen to when the textfield begins editing with the following delegate function:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//keep a member variable to store where the textField started
_yPositionStore = textField.frame.origin.y;
//If we begin editing on the text field we need to move it up to make sure we can still
//see it when the keyboard is visible.
//
//I am adding an animation to make this look better
[UIView beginAnimations:#"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
160 , //this is just a number to put it above the keyboard
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
and then in this callback you can return it to its original location:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:#"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
_yPositionStore ,
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
}
You will need to declare _yPositionStore as a member variable of CGFloat and have your textFields delegate be set to the view controller that owns it(either using Interface builder and dragging delegate to files owner or setting yourTextField.delegate = self)
I hope that helps
Do do this you need to follow next 2 steps:
- put all your UITextViews into UIScrollView
- register for keyboard notifications and slide your UIScrollView when keyboard appears
Both steps explained here: "Moving Content That Is Located Under the Keyboard"
You can call delegate methods of UITextfield and customize it according to requirement.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 110;
const float movementDuration = 0.3f;
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: #"aimation" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
yourView.frame= CGRectOffset(yourView.frame,0, movement);
[UIView commitAnimations];
}
For my applications I use the following code assuming that keyboardHeight is 216 for portrait and 162 for landscape mode:
#pragma mark - textField delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// keyboard is visible, move views
CGRect myScreenRect = [[UIScreen mainScreen] bounds];
int keyboardHeight = 216;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
int needToMove = 0;
CGRect frame = self.view.frame;
if (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height > (myScreenRect.size.height - keyboardHeight)) {
needToMove = (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height) - (myScreenRect.size.height - keyboardHeight);
}
frame.origin.y = -needToMove;
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// resign first responder, hide keyboard, move views
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
CGRect frame = self.view.frame;
frame.origin.y = 0;
[self.view setFrame:frame];
[UIView commitAnimations];
}
This formula takes into account the status bar, navigation bar and display size. And of course do not forget to set the delegate for your fields.
And with:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
you can hide the keyboard, when the user tap outside the field.
I have a little problem with uitextfield & uiscrollview, the problem is that when I click on uitextfield the keyboard hide the field, for solve this I use:
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
CGRect viewFrame = self.scroll.frame;
viewFrame.size.height -= 186;
[self.scroll setFrame:viewFrame];
return YES;
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[self.scroll setFrame:CGRectMake(0, 0, 320, 480)];
[self inserisciField];
[textField resignFirstResponder];
return YES;
}
and all work, the problem is with multiple field, with a field selected if I change the field without click return the scroll view has a wrong size/positon but if I click return, then I Select a field, press return, change field, ecc...all work.
I think that there is this problem because If I change field the scroll dimension was changed two time (the first time view frame.size.height -= 186, and another time with -=186) then I try to put this method
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.scroll setFrame:CGRectMake(0, 0, 320, 480)];
return YES;
}
where I set the default size of frame, but nothing.
use this -
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint point = CGPointMake(scroll.frame.origin.x, scroll.frame.origin.y + 186);
[self.scroll setContentOffset:point];
return YES;
}
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
CGPoint point = CGPointMake(0, 0);
[self.scroll setContentOffset:point];
}
You can use the contentoffset property of scrollview when u touch or edit a textfield as it is the best solution for your problem and touch and edit are the same process - only when you touch the field, it becomes editable.
I have a UITableView that is shorter than the window, therefore it does not need to scroll. However, it is long enough that when a text field in the bottom row is selected, the keyboard covers it.
I can't use scrollToRowAtIndexPath because the table is shorter than the window, so I was wondering what the correct way to bring it into view would be.
I was thinking about sliding the whole view up a set number of pixels, although that seems very bad form because it would break the UI if I added more rows to the table.
You should implement these methods in the concerned class :
- (void) textFieldDidBeginEditing:(UITextField *)myTextField
{
[self animateTextField:myTextField up:YES];
}
- (void) textFieldDidEndEditing:(UITextField *)myTextField
{
[self animateTextField:myTextField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int movement = (up ? -105 : 105);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
You have to adapt values (-105, 105 and 0.3f) to your situation.
You can have the whole tableView slide up by setting the height of the footerView. The Keyboard will move the table above for the height of the footer
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 70.0;
}
Pierre's code worked for me, but I removed the myTextField argument. It seemed unnecessary. I also changed this over to UITextViews because that's what I had in my code. Otherwise, thanks for the question and answers. I've been beating my head against the wall to solve this problem!
#pragma mark - Text View Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self animateTextViewUp:YES];
}
- (void) textViewDidEndEditing:(UITextView *)textView
{
[self animateTextViewUp:NO];
}
- (void) animateTextViewUp:(BOOL)up
{
int movement = (up ? -80 :80);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
i have list of UITextFields.
i want to move to the next text fields one by one after entering input in it.
since the keyboard is hiding the fields....
can you people show me that how can i accomplish that...
Thank a lot for your time.
(void)shiftViewUp:(BOOL)up
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if (movedUp)
{
if(rect.origin.y == 0){
rect.origin.y -= 85.0;
rect.size.height += 85.0;
}
}
else
{
if(rect.origin.y != 0.0){
rect.origin.y += 85.0;
rect.size.height -= 85.0;
}
}
self.view.frame = rect;
[UIView commitAnimations];
}
Basically you can call this method with BOOL YES when you begin editing the textfiled & with BOOL no when you end editing.
You just need to track which textfield is editing based on that you can adjust shifting (eg 85 here)
An alternative approach could be to use a UITableView. Then most of the controls are ready. But you can also make it all happen by yourself.
First put your UITextfields on a separate UIView. Make that UIView an outlet en link it properly. That way you can easily move the textfields up with an animation.
Then do something like this in the delegate methods:
// Sets the label of the keyboard's return key to 'Done' when the insertion
// point moves to the table view's last field.
//
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField tag] == myLastUITextField)
{
[textField setReturnKeyType:UIReturnKeyDone];
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField returnKeyType] != UIReturnKeyDone)
{
// If this is not the last field (in which case the keyboard's
// return key label will currently be 'Next' rather than 'Done'),
// just move the insertion point to the next field.
//
NSInteger nextTag = [textField tag] + 1;
UIView *nextTextField = [myTextView viewWithTag:nextTag];
// here starts your animation code
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = textFieldView.frame;
rect.origin.y = -44;
textFieldView.frame = rect;
[UIView commitAnimations];
// here ends your animation code. Play with it
[nextTextField becomeFirstResponder];
}
else
{
// do what you want to do with the last UITextfield
}
return YES;
}
`
I think it will not immediately work by copy-pasting but I hope it points in a direction. Good luck