2012-04-21 43 views
2

我正在gpsnote上做一個項目。如果任何人有任何信息生根粉這個應用程序PLZ指導我...如何在iPhone中使用鍵盤獲取文本字段sdk

我知道這個代碼BT,我想應該文本框和我的鍵盤來一起

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
} 

感謝

+0

yoou可以使用becomeFirstResponder鍵盤沿文本域。 – rishi 2012-04-21 07:01:37

回答

0

首先設置你的內容在滾動視圖,設置在viewDidLoad中滾動視圖:

[scrollView setContentSize : CGSizeMake (320, 1040)]; 

然後在viewWillAppear中添加以下通知:

對於鍵盤顯示

​​

對於鍵盤隱藏

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

以下是這兩個功能,通過通知稱爲

- (void)keyboardWasShown:(NSNotification*)aNotification { 

NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

double keyboardHeight = kbSize.height; 
double screenHeight = [UIScreen mainScreen].bounds.size.height - 20; 

if(textOrigin > screenHeight - keyboardHeight) 
{ 
    double sofset = textOrigin - (screenHeight - keyboardHeight); 
    CGPoint offset = scrollBackGround.contentOffset; 
    offset.y += sofset; 
    [scrollBackGround setContentOffset:offset animated:YES]; 
} 

} 


- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    [scrollBackGround setContentOffset:CGPointMake(0, 0) animated:YES]; 
} 

在keyboardWasShown功能我們正在做的就是獲取鍵盤的高度並檢查textField的y軸(即函數中的textOrigin)是否更大在鍵盤的Y軸上滑動包含我們文本字段的滾動視圖的內容。

NOw如何獲得文本字段的Y軸。爲此,您必須使用文本框的委託,對下列代表將觸發時,你的文本字段將成爲第一個響應者

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    textOrigin = scrollBackGround.frame.origin.y + textField.frame.origin.y + 20(it is status bar height) + yourNavigationBarheight; 

// Make sure to make textOrigin an ivar in your .h file 
} 

最後在keyboardWillBeHidden我們正在重置滾動視圖內容查看

+0

感謝您的寶貴提示.................... – user1347963 2012-04-23 07:35:14

+0

高興地幫助兄弟:) – superGokuN 2012-04-23 11:36:42

相關問題