回缩键盘

一、键值监听

   [[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];

    

}

posted @ 2015-12-10 11:50  莫莫smile1  阅读(171)  评论(0编辑  收藏  举报