Making the view slide up to make room for the keyb

2019-01-19 06:16发布

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 still see the text field you're typing in). How is it done?

5条回答
萌系小妹纸
2楼-- · 2019-01-19 06:33

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.

查看更多
小情绪 Triste *
3楼-- · 2019-01-19 06:36

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];  
}
查看更多
你好瞎i
4楼-- · 2019-01-19 06:39

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.

查看更多
乱世女痞
5楼-- · 2019-01-19 06:48

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.

查看更多
【Aperson】
6楼-- · 2019-01-19 06:50
  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];

}

查看更多
登录 后发表回答