美文网首页
iOS 新系统11.0 版本IQKeyboardManager导

iOS 新系统11.0 版本IQKeyboardManager导

作者: 一个萝卜X个坑 | 来源:发表于2017-09-23 14:13 被阅读198次

iOS新系统更新之后使用IQKeyboardManager导致布局出错,我的解决办法是禁用,自己写键弹起自适应
在视图将要出现的时候禁用IQKeyboardManager

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [[IQKeyboardManager sharedManager] setEnable:NO];
}

在视图将要消失的时候在恢复过来

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

在viewDidLoad方法中注册键盘通知

//增加监听,当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听,当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

然后再写入以下两个方法

#pragma mark当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int keyHeight = keyboardRect.size.height;
    
    
    
    //这里的offset的大小是控制着呼出键盘的时候view上移多少。比如上移20,就给offset赋值-20,以此类推。也可以根据屏幕高度的不同做一个if判断。

    CGFloat navheight = self.navigationController.navigationBar.frame.size.height;//导航栏目高度

CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];//状态栏高度
   
    float offset = keyHeight-navheight-rectStatus.size.height;
    
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyBoard"context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    float width = self.view.frame.size.width;
    
    float height = self.view.frame.size.height;
    
    CGRect rect = CGRectMake(0.0f, -offset , width, height);
    
    self.view.frame = rect;
    
    [UIView commitAnimations];
}
#pragma mark当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    CGFloat navheight = self.navigationController.navigationBar.frame.size.height;//导航栏目高度

CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];//状态栏高度
    float offset = navheight+rectStatus.size.height;
    
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyBoard"context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    float width = self.view.frame.size.width;
    
    float height = self.view.frame.size.height;
    
    CGRect rect = CGRectMake(0.0f, offset , width, height);
    
    self.view.frame = rect;
    
    [UIView commitAnimations];
}

我项目中的输入框是在底部的,你们根据自己的控件坐标设置

相关文章

网友评论

      本文标题:iOS 新系统11.0 版本IQKeyboardManager导

      本文链接:https://www.haomeiwen.com/subject/ljscextx.html