2011-04-12 62 views
0

在我的應用程序中,我有一些textfields通過鍵盤填充。其中有6個,最後兩個在屏幕的最下方,每當鍵盤出現時它都會隱藏起來。 有什麼建議?如何在鍵盤彈出時將視圖拖動一點點?

+0

或者看看這個偉大的教程由Matt:http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html – rckoenes 2011-04-12 14:39:17

+0

請參閱使用的代碼示例一步一步的博客教程.. http://iphonedevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html – Jhaliya 2011-04-12 14:46:29

回答

0

將textFields委託設爲自身並將以下代碼添加到textFieldDidBeginEditing和textFieldDidEndEditing中。

定義以下常量:

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 = 216; 
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 302; 

添加兩個變量在類的.h文件(常量已設置爲iPad調整它們爲iPhone)。 CGFloat animatedDistance;和UITextField * currentField;

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

    currentField = textField; 
    UIInterfaceOrientation orientation = 
    [[UIApplication sharedApplication] statusBarOrientation]; 

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

    CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view]; 
    if (orientation == UIInterfaceOrientationLandscapeLeft) 
    { 
     textFieldRect.origin.y = textFieldRect.origin.x; 
     textFieldRect.size.height = textFieldRect.size.width; 
     viewRect.size.height = viewRect.size.width; 
    } 
    else if (orientation == UIInterfaceOrientationLandscapeRight) 
    { 
     textFieldRect.origin.y =viewRect.size.width - textFieldRect.origin.x; 
     textFieldRect.size.height = textFieldRect.size.width; 
     viewRect.size.height = viewRect.size.width; 
    } 
    else if (orientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     textFieldRect.origin.y = viewRect.size.height - textFieldRect.origin.y; 
    } 

    CGFloat midline = textFieldRect.origin.y + 0.5 * 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; 
    } 
    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]; 

} 


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

    [textField resignFirstResponder]; 

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

    animatedDistance = 0; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 

    currentField = nil; 
} 
+0

它給了我一個錯誤,這是當前未聲明的字段和未聲明的動畫距離。這些是什麼?? – Ashutosh 2011-04-12 17:16:50

+0

哦,對不起。你必須聲明CGFloat animatedDistance;和UITextField * currentField;在你班級的.h中。我將編輯答案。感謝您指出。 – 2011-04-12 17:26:50

+0

完成!!!!!謝謝.. – Ashutosh 2011-04-12 19:00:28

0

使用UIScrollview,以便用戶可以向下滾動。你可以利用UITextField的inputAccessoryView來顯示下一個和上一個按鈕,點擊讓輸入跳轉到下一個文本框並滾動到它。

看看這個question and answers在SO,同樣的問題,不同的解決方案。

相關問題