美文网首页
ios 点击textfield,使textfield高度随键盘高

ios 点击textfield,使textfield高度随键盘高

作者: fulen | 来源:发表于2017-03-28 16:05 被阅读541次
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = QFMRGBColor(222, 222, 222);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)keyboardWillChangeFrame:(NSNotification *)note{
    CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    self.viewToBottm.constant = SCREEN_HEIGHT - frame.origin.y;
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }]; // textfield为xib创建,self.viewToBottm.constant为textfield距离view底部的距离
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

效果如图


相关文章

网友评论

      本文标题:ios 点击textfield,使textfield高度随键盘高

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