键盘显示隐藏对view进行操作

主要代码

#pragma mark - 键盘相关
#pragma mark - 初始化键盘相关通知事件
-(void)initKeyBoardNotice{
    keyboardIsShow = false;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

#pragma mark - 获取键盘尺寸
-(CGRect)getKeyBoardRect:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
    return keyboardRect;
}

- (void)keyboardWillShow:(NSNotification *)notification {
    
    if (!keyboardIsShow) {
        if (KeyBoardShowStyle==KeyBoardDealStyleChangeRootView) {
        //获取键盘高度
        CGRect keyboardRect =  [self getKeyBoardRect:notification];
        CGFloat keyboardHeight = keyboardRect.size.height;
        self.view.frame =CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height-keyboardHeight);
        }
        [self dealOtherWhenKeyboardShow];
        keyboardIsShow = true;
        NSLog(@"%f", self.view.frame.size.height);
    }
}

#pragma mark - 复写此方法,处理键盘显示时操作
-(void)dealOtherWhenKeyboardShow{
    
}

#pragma mark - 复写此方法,处理点击其他view的时候操作
-(void)dealOtherWhenTouchAnyWhere{
    
}

#pragma mark - 设置键盘显示时的处理方式
-(void)setKeyBoardShowStyle:(NSInteger)style{
    KeyBoardShowStyle = style;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    
    if (keyboardIsShow){
        if (KeyBoardShowStyle==KeyBoardDealStyleChangeRootView) {
            CGRect keyboardRect =  [self getKeyBoardRect:notification];
            CGFloat keyboardHeight = keyboardRect.size.height;
            self.view.frame =CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height+keyboardHeight);
        }
        keyboardIsShow = false;
    }
}

#pragma mark 设置点击其他地方的通知事件
- (void)setUpForDismissKeyboard{
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [self.view removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
    [self dealOtherWhenTouchAnyWhere];
}

以上主要代码会用到以下枚举参数用来处理键盘显示时的默认动作,以及viewcontroller中的两个参数

typedef NS_ENUM(NSInteger, KeyBoardDealStyle) {
    KeyBoardDealStyleChangeRootView = 0,//键盘显示时根view高度自动缩小一个键盘的高度,根view中直接是一个scrollview的视图适合使用,默认是这种方式
    KeyBoardDealStyleChangeNotRootView,//键盘显示时不改变根view大小
};
{
    Boolean keyboardIsShow;
    NSInteger KeyBoardShowStyle;
}

viewDidLoad里进行初始化

-(void)viewDidLoad{
    [super viewDidLoad];
    [self initKeyBoardNotice];
    [self setUpForDismissKeyboard];
}

设置键盘显示时的动作

  [self setKeyBoardShowStyle:KeyBoardDealStyleChangeNotRootView];

你可能感兴趣的:(键盘显示隐藏对view进行操作)