ios键盘遮挡UITextField问题

1、在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

中注册keyboard变化的通知

-(void) registerKeyboardNotification {

    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

    [defaultCenter addObserver:self

                      selector:@selector(keyboardWillShow:)//键盘将要出现时

                          name:UIKeyboardWillShowNotification

                        object:nil];

    [defaultCenter addObserver:self

                      selector:@selector(keyboardWillChanged:)//键盘将要变化时

                          name:UIKeyboardWillChangeFrameNotification

                        object:nil];

    [defaultCenter addObserver:self

                      selector:@selector(keyboardWillHide:)//键盘将要隐藏时

                          name:UIKeyboardWillHideNotification

                        object:nil];

}

keyboardWillShow方法的实现:

- (void)keyboardWillShow:(NSNotification *)notification {

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];

//UIKeyboardFrameEndUserInfoKey:动画结束后的键盘位置

    NSValue *value = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];

//keybordXY为私有属性,存储键盘的位置

//将[value CGRectValue].origin[value CGRectValue].origin所在视图转换到window中,返回window中的rect

    [self setKeybordXY:[window convertPoint:[value CGRectValue].origin toWindow:window]];

    [self adjust];

}

adjust方法的实现:

- (void)adjust {

    UIWindow *window = [[UIApplication sharedApplication] keyWindow];

    CGRect rect = [[responseView superview] convertRect:[responseView frame] toView:window];

    CGFloat curTy = adjustedView.transform.ty;

    if ((rect.origin.y + rect.size.height - curTy) >= keybordXY.y) {

        CGFloat offset = keybordXY.y - (rect.origin.y + rect.size.height - curTy + 20.f);

        [UIView animateWithDuration:.2f

                         animations:^(void){

                             [adjustedView setTransform:CGAffineTransformMakeTranslation(0.f, offset)];

                             [adjustedView layoutIfNeeded];

                         }

                         completion:^(BOOL finised){

                         }];

    } else {

        [UIView animateWithDuration:.2f

                         animations:^(void){

                             [adjustedView setTransform:CGAffineTransformMakeTranslation(0.f, 0.f)];

                             [adjustedView layoutIfNeeded];

                         }

                         completion:^(BOOL finised){

                         }];

    }

}

keyboardWillChanged方法的实现:

- (void)keyboardWillChanged:(NSNotification *)notification {  

}


keyboardWillHide方法的实现:

- (void)keyboardWillHide:(NSNotification *)notification {

    if (adjustedView) {

        [UIView animateWithDuration:.2f

                         animations:^(void){

                             [adjustedView setTransform:CGAffineTransformMakeTranslation(0.f, 0.f)];

                         }

                         completion:^(BOOL finised){}];

    }

    [self setResponseView:nil];

    [self setAdjustedView:nil];

}


-(void) unregisterKeyboardNotification {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

2、移除通知


你可能感兴趣的:(ios键盘遮挡UITextField问题)