美文网首页
UITableviewCell 上的文本框键盘遮挡

UITableviewCell 上的文本框键盘遮挡

作者: zeqinjie | 来源:发表于2016-05-11 16:02 被阅读169次

<pre>

  • (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHid) name:UIKeyboardWillHideNotification object:nil];

}

  • (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    }

pragma mark - function

  • (void)keyboardDidShow:(NSNotification *)notification{
    NSValue *value = [[notification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect rect = [value CGRectValue];
    UIView *firstResponder = [self.view.window performSelector:@selector(firstResponder)];
    CGRect tfrect = [firstResponder.superview convertRect:firstResponder.frame toView:self.view.window];
    if (tfrect.origin.y+self.view.frame.origin.y > rect.origin.y + firstResponder.frame.size.height) {
    CGFloat movedistance = tfrect.origin.y - rect.origin.y +firstResponder.frame.size.height;
    self.view.frame.origin.y -= movedistance;
    }
    }

-(void)keyboardDidHid{

 self.view.frame.origin.y = 0;//注意iOS7之后非透明的导航栏设置成64

}

</pre>

相关文章

网友评论

      本文标题:UITableviewCell 上的文本框键盘遮挡

      本文链接:https://www.haomeiwen.com/subject/fdrurttx.html