Keyboard hides UITextField. How to solve this?

I have a textField which is at bottom of the screen. When I begin editing, the keyboard appears and hides the textField.

Can anyone suggest how to solve this?

Answers


Generally you would put your content in a scroll view and move your scroll view up when the keyboard appears. There are many tutorials out there so google them. Here is one of them.


You can use following code in two diffrent method, call it when editing began and end, to move your view up and down,(just change the valuew that is been subtracted in yourView.frame.origin.y-100 to adjust according to your need)

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.5];      
    yourView.frame = CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y-100, yourView.frame.size.width, yourView.frame.size.height);

    [UIView commitAnimations];

See this code snippet. And use one UIButton to dismiss UIKeyboard.

- (void) animateTextField: (UITextField *)textField up:(BOOL) up
{
const int movementDistance = 200;
const float movementDuration = 0.4f;

int movement = (up ? -movementDistance : movementDistance);

up ? (self.button.enabled= YES) : (self.button.enabled = NO);

[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}

-(void)textFieldDidBeginEditing:(UITextField*) inTextField
{
[self animateTextField:mText up:YES];
[self addObservers];
}

- (void)addObservers 
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:@"UITextFieldTextDidChangeNotification" object:nil];    
}

- (void)textFieldDidChange:(NSNotification*)aNotification 
{
   NSLog(@"TextField data=%@",mText.text);
}   

- (IBAction) doneBtn:(id)sender
{
[mText resignFirstResponder ];
[self animateTextField:mText up:NO];
}

When you tap on textField keyboard up then your textField hide by it. so waht you need, you need to make your view up not keyboardresign is a solution. so for making you view up use

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField  delegate for the logic. now see the code

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    if(textField.tag==1)  //add tag to your textField to identify it
    {

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];

        CGRect viewFrame=self.view.frame;
        viewFrame=sinViewFrame.size.height+100;
        viewFrameorigin.y-=100; // as required
        self.view.frame=viewFrame;
        [UIView commitAnimations];

   }

}

hope it helps you.


I use auto layout constraint to solve this problem. I hold constraint as a property, and play with it when keyboard appears / disappears. I simply move views above the textfield out of screen, and textfield to the top. After finishing edit, I move them by the constraint to their original position.

-(void)keyboardWillShow:(NSNotification*)notification{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
    self.topSpaceLayoutConstraint.constant = -150;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
} completion:NULL];
}

-(void)keyboardWillHide:(NSNotification*)notification
{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
    self.topSpaceLayoutConstraint.constant = 10;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
} completion:NULL];
}

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpaceLayoutConstraint;

Need Your Help

Validating controls in ListView insert/edit templates

asp.net validation listview

I added a RequiredFieldValidator to my InsertItemTemplate, and it seems to be working fine. The problem I am having, however, is that now I cannot do anything else in the ListView (like edit or de...

Using Reflector, why can't I see the app.manifest embedded in my .NET app?

.net visual-studio windows-vista uac reflector

I have a WinForms .NET app that, according to the Vista Task Manager with UAC enabled, runs with virtualization "Disabled" even though I do not recall adding a manifest with "asInvoker" to the app.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.