除了頂部文本字段,當我開始鍵入時,iphone鍵盤會隱藏所有剩餘的文本字段。如何在鍵盤上方顯示活動文本字段,然後在隱藏鍵盤時將其恢復正常?如何在iPhone鍵盤顯示時顯示我的活動文本框?
回答
您必須在視圖頂部使用滾動視圖,並根據您選擇的文本字段實施數學contentOffset設置。 您選擇的文本框可以從委託方法獲得和偏移的計算可以用文本框框架
手錶UIKeyboardWillShowNotification
和UIKeyboardWillHideNotification
來完成。隨這些通知提供的NSNotification
對象將包含userInfo字典,其中包含有關鍵盤完成動畫後幀所具有的信息。根據這些通知調整您的佈局,以保持您想要被鍵盤覆蓋的東西。
在將所有文本字段置於滾動視圖之前,請使用以下代碼。
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
把你UITextField
在UIScrollView
和使用這樣的UIScrollView
contentOffSet
屬性:
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == txt1) //
{
[txt1 becomesFirstResponder];
scrMainView.contentOffset = CGPointMake(0, 20);
}
else if (textField == txt2) //
{
[txt1 resignFirstResponder];
[txt2 becomesFirstResponder];
scrMainView.contentOffset = CGPointMake(0, 60);
}
//And so on..
return YES;
}
希望,你會得到答案。
謝謝。
什麼是scrMainView?提到適當的細節,否則用戶不會知道你在暗示什麼。 'scrMainView'可能是scollview的權利? – 2013-03-19 09:45:44
是的。 scrMainView是我的滾動視圖,所以它會將內容偏移量設置得稍微高一點,所以用戶可以輕鬆地進行交互。 – 2013-03-19 09:47:03
然後請提供那些細節。只需看看我的編輯。它會清楚地說明你的答案。 – 2013-03-19 09:48:39
- 1. 顯示iPhone鍵盤
- 2. 活動開始時顯示軟鍵盤
- 3. 如何在保留滾動活動的同時顯示鍵盤?
- 4. 如何在輸入文本框時顯示鍵盤
- 5. Windows Phone文本框 - 鍵盤不顯示
- 6. 如何在顯示searchController時自動顯示鍵盤
- 7. 顯示Alt文本鍵盤
- 8. 如何在活動開始時顯示數字鍵盤?
- 9. iPhone鍵盤沒有顯示
- 10. 時顯示鍵盤
- 11. 在對話框活動中自動顯示軟鍵盤
- 12. 如何在iPhone中輸入文本時顯示鍵盤UItextfield的上邊?
- 13. 在UIWebView中點擊文本框時不顯示鍵盤
- 14. 鍵盤顯示時不顯示視圖?
- 15. 如何以編程方式顯示文本框的鍵盤?
- 16. 如何顯示鍵盤上的標籤欄時出現在iphone
- 17. EditText在移動時不顯示鍵盤
- 18. Android:如何在顯示軟鍵盤的同時顯示活動的上下文菜單
- 19. 如何在軟鍵盤顯示時選擇文本塊
- 20. 顯示虛擬鍵盤時點擊WebControl內的文本框
- 21. 如何顯示撥號盤活動
- 22. 顯示鍵盤
- 23. 如何在Android活動中顯示虛擬鍵盤
- 24. Android - 鍵盤顯示在活動下面,而不是活動
- 25. 如何在圖像具有焦點時顯示iPhone鍵盤?
- 26. 當我的活動開始時,如何顯示電話輸入軟鍵盤?
- 27. 如何在鍵盤顯示時禁用UIWebView的自動滾動
- 28. 當活動開始時沒有文本字段時顯示軟鍵盤
- 29. Phonegap如何自動顯示鍵盤
- 30. 如何手動顯示ios鍵盤?
請問你可以在這裏粘貼代碼嗎? – 2013-03-19 07:19:53
彈出虛擬鍵盤時向上移動整個視圖。看到這個。 http://stackoverflow.com/questions/15145341/undock-keyboard-programmatically – 2013-03-19 07:32:57