2014-04-03 39 views
0

我有幾個文本字段在屏幕上。當我向底部敲擊內部時,鍵盤會出現在它們的上方。UIScrollView不會移動視圖ontop的鍵盤iPad

我已經提到蘋果的文檔,並使用下面的代碼來嘗試解決這個問題。

// Call this method somewhere in your view controller setup code. 
- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillBeHidden:) 
               name:UIKeyboardWillHideNotification object:nil]; 

} 

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your app might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
     [self.scrollView scrollRectToVisible:activeField.frame animated:YES]; 
    } 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    activeField = textField; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    activeField = nil; 
} 

我想發生什麼,當鍵盤出現在滾動字段將向上移動的文本字段。任何幫助表示讚賞。

+0

寫我的代碼,你的問題就解決了..... –

回答

0

使用此代碼的移動視圖了..........

-(void) textFieldDidBeginEditing:(UITextField*)textField 
    { 
     [self slideViewUpForTextField:textField]; 
    } 

-(void) slideViewUpForTextField:(UITextField *)textField 
    { 

    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; 
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; 

    CGFloat midline = textFieldRect.origin.y + 3.0 * textFieldRect.size.height; 
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; 
    CGFloat heightFraction = numerator/denominator; 

    if (heightFraction < 0.0) 
     heightFraction = 0.0; 
    else if (heightFraction > 1.0) 
     heightFraction = 1.0; 

    UIInterfaceOrientation orientation = 
    [[UIApplication sharedApplication] statusBarOrientation]; 

    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) 
     animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
    else 
     animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 

    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y -= animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
    [self.view setFrame:viewFrame]; 
    [UIView commitAnimations]; 


} 

而寫這些的類 的頂部和#進口後「」 .....

 static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
     static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; 
     static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; 
     static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 158; 
     static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; 

這也寫....

-(void) textFieldDidEndEditing:(UITextField*) textField 
    { 

     CGRect viewFrame = self.view.frame; 
     viewFrame.origin.y += animatedDistance; 

     [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
    [self.view setFrame:viewFrame]; 
    [UIView commitAnimations]; 


    } 
+0

什麼我需要DECL是動畫的距離? – user2920762

+0

@ user2920762對不起,我忘了寫這個方法..... –

+0

我已經添加了所有這些,但需要聲明animatedDistance? – user2920762

相關問題