在我的應用程序中,當我單擊文本字段時,鍵盤隱藏它。請幫助我 - 當我點擊文本字段時,如何移動視圖。我在textFieldDidBeginEditing:
使用此代碼移動視圖,以便鍵盤不隱藏文本字段
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);
但它不起作用。
在我的應用程序中,當我單擊文本字段時,鍵盤隱藏它。請幫助我 - 當我點擊文本字段時,如何移動視圖。我在textFieldDidBeginEditing:
使用此代碼移動視圖,以便鍵盤不隱藏文本字段
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);
但它不起作用。
你可以做到以下幾點,但首先要確保你已經設置的UITextField代表你的自我和
#define kOFFSET_FOR_KEYBOARD 350;
在頂部。這是你想要多遠視圖要移位
//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
[UIView setAnimationBeginsFromCurrentState:YES];
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.
if (rect.origin.y == 0) {
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
//rect.size.height += kOFFSET_FOR_KEYBOARD;
}
}
else
{
if (stayup == NO) {
rect.origin.y += kOFFSET_FOR_KEYBOARD;
//rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notif {
[self setViewMovedUp:NO];
}
- (void)keyboardWillShow:(NSNotification *)notif{
[self setViewMovedUp:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
stayup = YES;
[self setViewMovedUp:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
stayup = NO;
[self setViewMovedUp:NO];
}
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:self.view.window];
}
- (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];
}
你不應該相信textFieldDidBeginEditing:
調整爲鍵盤,因爲這種方法將被調用,即使用戶使用物理鍵盤,其中屏幕鍵盤就會打字不被顯示。
取而代之的是聽取UIKeyboardWillShowNotification
,這隻有在鍵盤實際顯示時纔會觸發。您需要執行三個步驟:
userInfo
字典中確定鍵盤的實際大小。尺寸將不同於風景/肖像以及不同的設備。contentInset
。你可以做動畫,通知甚至會告訴你鍵盤動畫的持續時間。你會發現更多的信息和示例代碼here
的可能重複[如何使一個的UITextField向上移動時,鍵盤的存在(http://stackoverflow.com/questions/1126726/how-to -make-a-uitextfield-move-up-when-keyboard-is-present) – 2011-05-13 05:54:20