解决虚拟键盘挡住UITextField

方法一:

http://blog.csdn.net/xiaotanyu13/article/details/7711954

因为屏幕太小的缘故,一个键盘跳出来总是把输入框挡住,所以需要移动屏幕来匹配键盘

#pragma mark -

#pragma mark 解决虚拟键盘挡住UITextField的方法

- (void)keyboardWillShow:(NSNotification *)noti

{       

    //键盘输入的界面调整       

    //键盘的高度

    float height = 216.0;               

    CGRect frame = self.view.frame;       

    frame.size = CGSizeMake(frame.size.width, frame.size.height - height);       

    [UIView beginAnimations:@"Curl"context:nil];//动画开始         

    [UIView setAnimationDuration:0.30];          

    [UIView setAnimationDelegate:self];         

    [self.view setFrame:frame];        

    [UIView commitAnimations];

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{       

    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.       

    NSTimeInterval animationDuration = 0.30f;       

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       

    [UIView setAnimationDuration:animationDuration];       

    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.widthself.view.frame.size.height);  

    //CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);

    self.view.frame = rect;

    [UIView commitAnimations];

    [textField resignFirstResponder];

    return YES;       

}

- (void)textFieldDidBeginEditing:(UITextField *)textField

{       

    CGRect frame = textField.frame;

    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216

    NSTimeInterval animationDuration = 0.30f;               

    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];               

    [UIView setAnimationDuration:animationDuration];

    float width = self.view.frame.size.width;               

    float height = self.view.frame.size.height;       

    if(offset > 0)

    {

        CGRect rect = CGRectMake(0.0f, -offset,width,height);               

        self.view.frame = rect;       

    }       

    [UIView commitAnimations];               

}

#pragma mark -

只要在代码中加入这三个文件,然后将自身delegate
如右上角  就可以实现屏幕的移动了,
但是这里经常会有屏幕移动后不能返回的问题,这里的解决方案就是

- (IBAction)backgroundTap:(id)sender {

    NSTimeInterval animationDuration = 0.30f;       

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       

    [UIView setAnimationDuration:animationDuration];       

    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.widthself.view.frame.size.height);       

    self.view.frame = rect;

backgroundTap函数中添加这些代码,这样屏幕就会返回正常了。
效果:
方法二:
#define kOFFSET_FOR_KEYBOARD 80.0

-(void)keyboardWillShow {
    // Animate the current view out of the way
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if ([sender isEqual:mailTf])
    {
        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
        }
    }
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}


- (void)viewWillAppear:(BOOL)animated
{
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

效果:

问题:Apple recommends against using a constant for the size of the keyboard?

posted @ 2013-10-22 17:35  时光独白  阅读(465)  评论(0编辑  收藏  举报