码迷,mamicode.com
首页 > 移动开发 > 详细

键盘将出现键盘将隐藏并且view上下移动

时间:2015-05-23 16:43:27      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:


-(void)viewWillAppear:(BOOL)animated{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

 

-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


-(void) keyboardWillShow:(NSNotification *) aNotification {
    
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    NSLog(@"%d",height);
    //跳转到详细的视图控制器
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
        
        //改变位置
        self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, height, 0);
        
    }];
    
}

-(void) keyboardWillHide:(NSNotification *) aNotification
{
    [UIView animateWithDuration:0.3 animations:^{
        
        
        //改变位置
        self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, 0, 0);
        
    }];
    
}

或者改view得frame的纵坐标

例如

keyboardWillShow:

self.view.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64);

keyboardWillHide:

self.view.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64);

键盘将出现键盘将隐藏并且view上下移动

标签:

原文地址:http://www.cnblogs.com/huangzs/p/4524402.html

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