开发者

Making the view slide up to make room for the keyboard?

I just started learning to program iPhone apps and I can't seem to figure out how to make the view slide out of the way when the keyboard appears (so you can s开发者_如何转开发till see the text field you're typing in). How is it done?


If it is OK visually, the easiest is to move the entire self.view.frame and then move it back down when finished.

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3f;

- (void) animateForToNewYPosition:(int)newYPosition {
    // move for kdb
    if (self.view.frame.origin.y == newYPosition) {
        return;
    }

    // start animation
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];  

    // move it
    self.view.frame.origin.y = newYPosition;

    [UIView commitAnimations];  
}


One way to do it is to contain everything in a UIScrollView and then scroll the contents upward. Another way is to move the view yourself, usually with the help of Core Animation to make it look nice.

A good place to start is with the documenation. There's even a section helpfully labelled Moving Content That Is Located Under the Keyboard which will point you in the right direction.


suppose you need to move up view on a text field which tag is 4(when you have more than 1 txt fields and one of them covered by the keyboard) then use textField delegate method

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField   
{
if(textField.tag==4)
    CGRect viewFrame;
    viewFrame=self.view.frame;
    if(viewFrame.origin.y==-100)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];
        viewFrame.origin.y+=100;
        self.view.frame=viewFrame;
        [UIView commitAnimations];
    }

}

this moves your view. now for moving down you need code in textField anothe delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   if(textField.tag==4)
{
 CGRect viewFrame;
    viewFrame=self.view.frame;
    if(viewFrame.origin.y==-100)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:.3];
        viewFrame.origin.y+=100;
        self.view.frame=viewFrame;
        [UIView commitAnimations];
    }
}
}

In case of textview you need to a button and for moving up your view you need this delegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

and use same code as textField

And moving down you need a button either in navigation bar or add in toolbar and set that tool bar over the keyboard by same animation.For button you need same code for moving down which is applicable for textField.

Hope this helps you.


I found that using the keyboard notifications worked better for my application than using the UITextField delegate protocol of textFieldDidBeginEditing and textFieldDidEndEditing. The notifications are keyboardWillShow and keyboardWillHide. One can test for a UITextField or UITextView that would require the view to move withing these notifications and then conditionally move the view. The advantage in my application is that I have many UITextTields and the notifications make it easier to keep the view in place above the keyboard when editing moves from one field to the other.


  http://objectivecwithsuraj.blogspot.in/2012/06/making-view-slide-up-to-make-room-for.html

  Add a UIScrollview - scrollview to your UIView and set delegates for UITextFields & 
     UIScrollview

  - (BOOL)textFieldShouldReturn:(UITextField *)textField 
  {
     if (textField == txtFieldName)
   {
         [txtFieldCellNo becomeFirstResponder];
   }
   else if (textField == txtFieldCellNo)
   {
         [txtFieldEmail becomeFirstResponder];
   }
   else
  {
       [textField resignFirstResponder];

  }
return YES;
}

 - (void)textFieldDidBeginEditing:(UITextField *)textField
 {
    [self animateTextField:txtFieldName up:YES];
 }


- (void)textFieldDidEndEditing:(UITextField *)textField
{
   [self animateTextField:txtFieldEmail up:NO];
}


- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 80; 
    const float movementDuration = 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];

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜