iOS鍵盤彈出的處理

#pragma mark -view自動調整位置

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

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

    [window setBackgroundColor:[UIColor whiteColor]];

    NSDictionary *dict = [notification userInfo];

    NSValue *value = [dict objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGFloat keyboardHeight = value.CGRectValue.size.height;

    NSNumber *duration = [dict objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [dict objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    if (self.nameText.isFirstResponder) {

    }else if(self.wayText.isFirstResponder){

         CGFloat  offsetTop = viewHeightCGRectGetMaxY(self.wayHolder.frame);

        if (offsetTop < keyboardHeight) {

            offsetTop = offsetTop - keyboardHeight;

            [UIView animateWithDuration:duration.floatValue animations:^{

                [UIView setAnimationBeginsFromCurrentState:YES];

                [UIView setAnimationCurve:curve.intValue];

                self.view.frame = CGRectMake(0, offsetTop, self.view.frame.size.width, self.view.frame.size.height);

            }];

        }

        

    }else if (self.integralText.isFirstResponder){

        CGFloat  offsetTop = viewHeightCGRectGetMaxY(self.integralHolder.frame);

        if (offsetTop < keyboardHeight) {

            offsetTop = offsetTop - keyboardHeight;

            [UIView animateWithDuration:duration.floatValue animations:^{

                [UIView setAnimationBeginsFromCurrentState:YES];

                [UIView setAnimationCurve:curve.intValue];

                self.view.frame = CGRectMake(0, offsetTop, self.view.frame.size.width, self.view.frame.size.height);

            }];

        }

        

    }else if (self.addressText.isFirstResponder){

        CGFloat  offsetTop = viewHeightCGRectGetMaxY(self.addressHolder.frame);

        if (offsetTop < keyboardHeight) {

            offsetTop = offsetTop - keyboardHeight;

            [UIView animateWithDuration:duration.floatValue animations:^{

                [UIView setAnimationBeginsFromCurrentState:YES];

                [UIView setAnimationCurve:curve.intValue];

                self.view.frame = CGRectMake(0, offsetTop, self.view.frame.size.width, self.view.frame.size.height);

            }];

        }

        

    }else if (self.declareTextView.isFirstResponder){

        CGFloat  offsetTop = -keyboardHeight+64;

        [UIView animateWithDuration:duration.floatValue animations:^{

            [UIView setAnimationBeginsFromCurrentState:YES];

            [UIView setAnimationCurve:curve.intValue];

            self.view.frame = CGRectMake(0, offsetTop, self.view.frame.size.width, self.view.frame.size.height);

        }];

    }

   }

#pragma mark -keyboardWillHide

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

    NSDictionary *dict = [notification userInfo];

    NSNumber *duration = [dict objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSNumber *curve = [dict objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    [UIView animateWithDuration:duration.doubleValue animations:^{

        [UIView setAnimationCurve:curve.intValue];

        self.view.frame = CGRectMake(0, viewOriginY, self.view.frame.size.width, self.view.frame.size.height);

    }];

}

我是這樣實現的  點擊不同的控件 都會根據控件的具體位置計算view需要上移的位置 但是過程中出現了一個BUG,就是每次view上移之前都會先出現一個黑色的方塊,喲過戶體驗非常的不好,我目前是這個解決的,通過設置window的背景色爲白色,就是跟你view一樣的顏色即可,這樣看起來感覺不出有什麼BUG   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章