2012-03-23 49 views
0

如何創建停留在屏幕底部的可移動UItextfield,當鍵盤出現時,它將移動到iphone應用程序中的鍵盤頂部?如何在Xcode 4.2中做到這一點? 就像在WhatsApp和Skype聊天中一樣。 我想用來輸入字符串到表。如何創建可移動的UItextfield?

+1

檢查此[鏈接](https://github.com/brandonhamilton/inputtoolbar)其開源項目做你想做的。 – 2012-03-24 00:48:59

+0

抱歉,但我認爲這幾乎不適用於XCode 4.2 storyboareding – SultanSh 2012-03-24 16:04:18

回答

1

您應該將viewController註冊爲鍵盤通知的偵聽器。當鍵盤出現時,用戶字典會觸發通知。字典將包含有用的信息,例如相對於屏幕的鍵盤位置,用於將textFields框架動畫到新位置。退房的文件:

http://developer.apple.com/library/ios/#DOCUMENTATION/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

具體來說,你想要的通知是:

[[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(keyboardWasShown:) 
      name:UIKeyboardDidShowNotification object:nil]; 

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

你要在viewDidLoad中在大多數情況下添加這些。不要忘記在完成後稍後註銷通知(removeObserver :),例如在viewDidUnload中。

0

您可以按照Hubert提到的方法使用通知,或者您可以不斷檢查UITextField是否是第一響應者。如果它是第一響應者,這意味着文本字段被選中並且鍵盤將影響該文本字段。

-(void)viewDidLoad { 

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(checkTextField) userInfo:nil repeats:YES]; 

} 

-(void)checkTextField { 

    if (textField isFirstResponder) { 

     //the text field has been tapped and the keyboard will come up, so animate the text field moving up here 


    } else { 

     //the text field is not selected, so it should be in its original position 

    } 

}