美文网首页TextField
TextField随键盘弹出上移

TextField随键盘弹出上移

作者: 欲速则不达 | 来源:发表于2016-03-22 17:09 被阅读219次

UITextField的代理方法

  • (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
    //iphone键盘默认高度216
    CGFloat offset = self.view.frame.size.height - (textField.frame.origin.y + textField.frame.size.height + 216 + 50);
    if (offset <= 0) {
    [UIView animateWithDuration:0.3 animations:^{
    CGRect frame = self.view.frame;
    frame.origin.y = offset;
    self.view.frame = frame;
    }];
    }
    return YES;
    }

  • (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
    // 输完后恢复原来的状态
    [UIView animateWithDuration:0.3 animations:^{
    CGRect frame = self.view.frame;
    frame.origin.y = 0.0;
    self.view.frame = frame;
    }];
    return YES;
    }

相关文章

网友评论

    本文标题:TextField随键盘弹出上移

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