iOS 键盘弹出遮挡UITextField解决方法

来源:互联网 发布:oracle java认证考试 编辑:程序博客网 时间:2024/06/04 17:55

 //增加监听,当键退出时收出消息

    [[NSNotificationCenterdefaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotification

                                               object:nil];




//开始编辑时视图上移如果输入框不被键盘遮挡则不上移。

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

    

    CGFloat rects;

    if( 1==textField.tag ||2==textField.tag ||3==textField.tag||4==textField.tag)

    {

        rects = self.view.frame.size.height - (_topview.frame.origin.y +40*textField.tag +216 +50);


    }

    else

    {

        rects = self.view.frame.size.height - (_centerView.frame.origin.y + 40*(textField.tag-4)  +216 +50);


    }

    

    if (rects <=0) {

        

        [UIViewanimateWithDuration:0.3animations:^{

            

            CGRect frame =self.view.frame;

            

            frame.origin.y = rects;

            

            self.view.frame = frame;

            

        }];

        

    }

    returnYES;

    

}

//当键退出时调用

- (void)keyboardWillHide:(NSNotification *)aNotification

{

    [UIViewanimateWithDuration:0.3animations:^{

        

        CGRect frame =self.view.frame;

        

        frame.origin.y =64.0;

        

        self.view.frame = frame;

        

    }];

}

0 0