首页 > 移动平台 > 详细

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

时间:2018-06-02 00:10:49      阅读:325      评论:0      收藏:0      [点我收藏+]
-(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-利用键盘通知处理键盘出现时遮挡控件问题

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!