当前位置:   article > 正文

IOS 键盘弹出遮挡输入框使view上移_ios 设置了frame ,但键盘弹起时,view直接顶格了

ios 设置了frame ,但键盘弹起时,view直接顶格了

有时候输入框在屏幕下方,键盘弹出遮挡了输入框有种盲输入的赶脚,那我们就可以把视图向上移动让它露出来。


首先在viewDidLoad中注册键盘显隐通知

//注册键盘弹出通知

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification

                                               object:nil];

    //注册键盘隐藏通知

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotification

                                               object:nil];

//键盘弹出后将视图向上移动

-(void)keyboardWillShow:(NSNotification *)note

{

    NSDictionary *info = [note userInfo];

    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    //目标视图UITextField

    CGRect frame = passwordTF.frame;

    int y = frame.origin.y + frame.size.height - (self.view.frame.size.height - keyboardSize.height);

    NSTimeInterval animationDuration = 0.30f;

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

    [UIView setAnimationDuration:animationDuration];

    if(y > 0)

    {

        self.view.frame = CGRectMake(0, -y, self.view.frame.size.width, self.view.frame.size.height);

    }

    [UIView commitAnimations];

}


//键盘隐藏后将视图恢复到原始状态

-(void)keyboardWillHide:(NSNotification *)note

{

    NSTimeInterval animationDuration = 0.30f;

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

    [UIView setAnimationDuration:animationDuration];

    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    [UIView commitAnimations];

}


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/915384
推荐阅读
相关标签
  

闽ICP备14008679号