码迷,mamicode.com
首页 > 其他好文 > 详细

回缩键盘

时间:2015-12-10 13:01:54      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

一、键值监听

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

    

}

回缩键盘

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!