2011-05-05 60 views
4

我剛開始學習編程iPhone應用程序,我似乎無法弄清楚在鍵盤出現時如何讓視圖滑出來(所以你仍然可以看到你輸入的文本字段)。它是如何完成的?使視圖向上滑動,爲鍵盤騰出空間?

+2

Michael Tyson已經編寫了一個自動化的組件:[用於移動文本字段的一種通用解決方案,不受鍵盤影響](http://atastypixel.com/blog/a-drop-in-universal - 溶液換移動文本字段,遙遙的最鍵盤/)。 – 2011-05-05 03:27:47

+0

該鏈接真的很好!謝謝! – BlackHatSamurai 2013-05-21 03:33:52

回答

2

如果視覺上沒問題,最簡單的方法是移動整個self.view.frame,然後在完成後將其移回原位。

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3f; 

- (void) animateForToNewYPosition:(int)newYPosition { 
    // move for kdb 
    if (self.view.frame.origin.y == newYPosition) { 
     return; 
    } 

    // start animation 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [UIView beginAnimations:nil context:context]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    // move it 
    self.view.frame.origin.y = newYPosition; 

    [UIView commitAnimations]; 
} 
+0

謝謝,正是我需要的! – Ryan 2011-05-06 00:50:46

+0

如何給這個方法打電話? – kendotwill 2015-01-22 03:59:11

+0

使用UITextField委託,你可以把調用animateForToNewYPosition:在委託方法textFieldDidBeginEditing:一個很好的例子/答案在這裏:http://stackoverflow.com/a/17887070/203960 – dredful 2015-01-22 05:09:20

1

這樣做的一種方法是在UIScrollView中包含所有內容,然後向上滾動內容。另一種方法是自己移動視圖,通常在Core Animation的幫助下,使其看起來不錯。

一個很好的起點是文檔。甚至還有一個標有Moving Content That Is Located Under the Keyboard的標籤,它會指引您正確的方向。

+0

好的,我如何使用UIScrollView來做到這一點? – Ryan 2011-05-05 03:17:11

+0

在上面添加了一個鏈接,告訴你你需要知道什麼。 – Caleb 2011-05-05 03:25:10

0

假設你需要移動起來視圖上的文本字段,標籤是4(當你有超過1個TXT領域,並通過鍵盤覆蓋其中的一個),然後使用文本框的委託方法

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
if(textField.tag==4) 
    CGRect viewFrame; 
    viewFrame=self.view.frame; 
    if(viewFrame.origin.y==-100) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:.3]; 
     viewFrame.origin.y+=100; 
     self.view.frame=viewFrame; 
     [UIView commitAnimations]; 
    } 

} 

這會移動您的視圖。現在向下移動,你需要在文本框代碼anothe委託方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    if(textField.tag==4) 
{ 
CGRect viewFrame; 
    viewFrame=self.view.frame; 
    if(viewFrame.origin.y==-100) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:.3]; 
     viewFrame.origin.y+=100; 
     self.view.frame=viewFrame; 
     [UIView commitAnimations]; 
    } 
} 
} 

在TextView的情況下,你需要一個按鈕,移動你的觀點,你需要這個代表

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView 

,並使用相同的代碼textField

向下移動需要在導航欄中添加一個按鈕,或者在工具欄中添加按鈕,並通過相同的動畫在鍵盤上設置該工具欄。對於按鈕,您需要使用相同的代碼向下移動,這適用於textField。

希望這有助於你。

+0

我把這個在ViewController文件中,對?它應該被自動調用嗎?它不適合我... – Ryan 2011-05-08 03:54:35

+0

您需要爲textField委託與文件所有者建立連接,或者以編程方式編寫yourTxtField.delegate = self;然後這些由self調用的委託方法 – Ishu 2011-05-09 03:53:29

0
http://objectivecwithsuraj.blogspot.in/2012/06/making-view-slide-up-to-make-room-for.html 

    Add a UIScrollview - scrollview to your UIView and set delegates for UITextFields & 
    UIScrollview 

    - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    { 
    if (textField == txtFieldName) 
    { 
     [txtFieldCellNo becomeFirstResponder]; 
    } 
    else if (textField == txtFieldCellNo) 
    { 
     [txtFieldEmail becomeFirstResponder]; 
    } 
    else 
    { 
     [textField resignFirstResponder]; 

    } 
return YES; 
} 

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self animateTextField:txtFieldName up:YES]; 
} 


- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [self animateTextField:txtFieldEmail up:NO]; 
} 


- (void) animateTextField: (UITextField*) textField up: (BOOL) up 
{ 
    const int movementDistance = 80; 
    const float movementDuration = 0.3f; 
    int movement = (up ? -movementDistance : movementDistance); 
    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    self.view.frame = CGRectOffset(self.view.frame,0, movement); 
    [UIView commitAnimations]; 

}

0

我發現,使用鍵盤工作的通知我的應用程序比使用textFieldDidBeginEditing和textFieldDidEndEditing的的UITextField委託協議更好。通知是keyboardWillShow和keyboardWillHide。可以測試UITextField或UITextView,這需要視圖隨這些通知一起移動,然後有條件地移動視圖。我的應用程序的優點是我有很多UITextTields,通知可以讓編輯從一個字段移動到另一個字段時,更容易將視圖保留在鍵盤上方。

相關問題