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

处理键盘弹出

时间:2015-05-20 22:18:09      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

//======== 监听键盘的弹出事件 ========

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];



// 当键盘的位置大小发生改变时触发

- (void)keyboardWillChangeFrame:(NSNotification *)note

{

    // 1. 获取键盘当前的高度

    NSLog(@"%@", note.userInfo);

    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;

    CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    CGFloat keyboardY = rect.origin.y;

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    

    /// 2.将当前ViewController的View整体向上平移一个"键盘的高度"

    [UIView animateWithDuration:duration animations:^{

        self.view.transform = CGAffineTransformMakeTranslation(0, keyboardY - screenH);

    }];

    

}

 
///记得移除通知
- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

 

处理键盘弹出

标签:

原文地址:http://www.cnblogs.com/songxing10000/p/4518259.html

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