回缩键盘

一、键值监听

   [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification

                                               object:nil];

    //增加监听,当键退出时收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillHide:)

                                                 name:UIKeyboardWillHideNotification

                                               object:nil];

#pragma mark ------- Action

- (void)keyboardWillShow:(NSNotification *)aNotification

{

    //获取键盘的高度

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    CGFloat keyHeight = keyboardRect.size.height;

    self.view.top=100-keyHeight;

    //    if ([_emailText isFirstResponder]) {

    //        if(kDeviceHeight<_emailText.bottom+keyHeight+64)

    //            self.view.top=64+(kDeviceHeight-_emailText.bottom-keyHeight-64);

    //    }

    

}

- (void)keyboardWillHide:(NSNotification *)aNotification

{

    //获取键盘的高度

    self.view.top=64;

}

二、手势监听

 UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];

    tap1.cancelsTouchesInView = NO;

    [self.view addGestureRecognizer:tap1];

#pragma mark----点击空白处收起键盘

-(void)viewTapped:(UITapGestureRecognizer*)tap1

{

    

    [self.view endEditing:YES];

    

}

原文地址:https://www.cnblogs.com/momosmile/p/5035485.html