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

ios-利用键盘通知处理键盘出现时遮挡控件问题

时间:2018-06-02 00:30:50      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:bsp   float   obj   load   key   keyboard   rsize   ble   rect   

-(void)viewDidLoad {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    //注册键盘显示通知
    [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    //注册键盘隐藏通知
    [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)viewDidDisappear:(BOOL)animated
{

    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void) keyboardWillShow:(NSNotification *) notification{
    
    NSDictionary *info;
    CGSize kbSize;
    double kbheight;
    CGFloat Oversize;
    double duraction;
    
    info = notification.userInfo;
    kbSize = [[info objectForKey: UIKeyboardFrameEndUserInfoKey]
CGRectValue].size;
    kbheight = kbSize.height;
    
    Oversize = (self.view.frame.size.height - kbheight) - (textfield.frame.origin.y + textfield.frame.size.height+5);
    
    duraction =  [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    if (Oversize < 0){
        [UIView animateWithDuration: duraction animations:^{
            self.view.frame = CGRectMake(0.0f, Oversize, self.view.frame.size.width, self.view.frame.size.height);
        }];
    }
}

-(void) keyboardWillHide:(NSNotification *) notification{
    
    double duraction = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duraction animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

 

ios-利用键盘通知处理键盘出现时遮挡控件问题

标签:bsp   float   obj   load   key   keyboard   rsize   ble   rect   

原文地址:https://www.cnblogs.com/cccliche/p/9123854.html

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