UI进阶 —— 收起键盘方法

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//  设置代理
    self.inputView.delegate = self;
    self.tableView.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification{
//   键盘消息机制配置打印,用来测试、查看键盘属性
    NSLog(@"%@", notification.userInfo);
    CGRect keyFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//   设置新Y点,计算方法为,键盘的Y点 - 视图高度 = 视图新y点
    CGFloat y = keyFrame.origin.y - self.view.frame.size.height;
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y = y;
//  获取键盘动画时间,用以与view时间相同
    CGFloat time = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//  播放动画方法
    [UIView animateWithDuration:time delay:0 options:7<<16 animations:^{
        self.view.frame = viewFrame;
    } completion:nil];
    
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

@end




UIKeyboardWillShowNotification / / 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification / / 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification / / 键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值