键盘抬起落下,输入框相应抬起落下

-(void)myViewLayout

{

    // 旋转不能用frame 要用bounds

    self.inputView.frame = CGRectMake(0, self.view.bounds.size.height- self.bottomLayoutGuide.length-45, self.view.bounds.size.width, 45);

}

-(void)viewDidLayoutSubviews

{

    [self myViewLayout];

}

-(void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    [center addObserver:self selector:@selector(keyboardAppear:) name:UIKeyboardWillShowNotification object:nil];

    [center addObserver:self selector:@selector(keyboardDisappear:) name:UIKeyboardWillHideNotification object:nil];

}


- (void)keyboardAppear:(NSNotification *)notification

{

    //1. 获取键盘的高度

    NSDictionary *userinfo = notification.userInfo;

    NSValue *value = [userinfo valueForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardFrame = [value CGRectValue];

    UIWindow *window = [[UIApplication sharedApplication] delegate].window;

    // rectview中转换到当前视图中,返回在当前视图中的rect

    keyboardFrame = [self.view convertRect:keyboardFrame fromView:window];

    

    //2. 计算输入框的frame

    CGRect frame = self.inputView.frame;

    frame.origin.y = self.view.bounds.size.height - keyboardFrame.size.height - frame.size.height;

    //3. 动画

    //NSTimeInterval duration = [userinfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] + 0.1;

    // 键盘的动画时间+0.1

    NSTimeInterval duration = [[userinfo valueForKey: UIKeyboardAnimationDurationUserInfoKey] doubleValue] + 0.1;

    

    UIViewAnimationOptions options = [userinfo[UIKeyboardAnimationCurveUserInfoKey]unsignedIntegerValue];

    [UIView animateWithDuration:duration

                          delay:0

                        options:options

                     animations:^{

                         self.inputView.frame = frame;

                     } completion:nil];

}


- (void)keyboardDisappear:(NSNotification *)notification

{

    CGRect frame = self.inputView.frame;

    frame.origin.y = self.view.bounds.size.height - frame.size.height - self.bottomLayoutGuide.length;

    

    self.inputView.frame = frame;

    

}


- (IBAction)didInput:(UITextField *)sender

{

    [sender resignFirstResponder];

}


- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值