uitextfield 键盘类型_UITextField 键盘弹出问题
有朋友問了一些關于iPhone鍵盤的問題, 那么總結一下, 在 iOS 程序中當想要在文本框中輸入數據,點擊文本框會打開鍵盤。對于 iPad 程序,其鍵盤有一個按鈕可以用來關閉鍵盤,但是 iPhone 程序中的鍵盤卻沒有這樣的按鈕,不過我們可以采取一些方法關閉它。例如,我們可以實現按下 Return [Keyboard Type為defult] (有時也是 Done、Research 等)關閉鍵盤,或更人性化的,輕觸背景關閉鍵盤。
1、首先講一下按下Return鍵關閉鍵盤。
當按下鍵盤的 Return 鍵,會產生一個 Did End On Exit 事件,此時我們告訴文本框要放棄控件,于是鍵盤就消失了。在這個基礎上,實現輕觸 Return 關閉鍵盤,步驟為:
(1)在 ViewController.h 中聲明一個方法:
- (IBAction)textFiledReturnEditing:(id)sender;
(2)在 ViewController.m 中實現這個方法:
- (IBAction)textFiledReturnEditing:(id)sender
{
[sender resignFirstResponder];
}
Snip20160511_2.png
Snip20160511_1.png
所謂 First Responder 譯為第一響應者,指的就是用戶當前正在與之交互的控件,。當用戶使用鍵盤時,First Responder 就是這個鍵盤,resignFirstResponder 方法,顧名思義,就是放棄 First Responder 。
2、下面介紹更人性化的方法,輕觸背景關閉鍵盤。
跟上面的步驟差不多,首先定義一個方法,然后實現這個方法,接下來將指定的控件映射到這個方法,并選擇好所觸發的事件。不同的是,這次我們要選擇的控件不是上邊的文本框,而是視圖 View 本身。
(1)在 ViewController.h 文件中添加方法聲明代碼:
- (IBAction)backgroundTap:(id)sender;
(2)在ViewController.m中實現這個方法:
- (IBAction)backgroundTap:(id)sender
{
[self.textField resignFirstResponder];
}
需要說明的是,[self.textField resignFirstResponder]表示,如果textField有FirstResponder的話就放棄它,我們不用先判斷firstField是否有,這條語句完全正確。
(3)讓 View 映射到這個方法,不過事先,我們先要改變 View 的類型。
打開xib,選中 View ,打開 Identity Inspector ,在 class 中選擇 UIControl :
Snip20160511_3.png
(4)在 Connector Inspector 中找到 Touch Down ,從它右邊的圓圈中拉出映射線,映射到 ViewController.h 的 backgroundTap 方法,如下圖:
Snip20160511_4.png
如此, 打開鍵盤之后,在空白區域點擊一下,鍵盤就會向下收起來。
在論壇上也有只寫一個backgroundTap 函數,然后將組件的事件和屏幕的事件指向同一個函數。其實這兩個方法都是可以用的,但是呢,我更加傾向于使用同一個函數的方法,原因就要牽扯到第二個方面的知識:
解決虛擬鍵盤擋住UITextField的方法因為屏幕太小的緣故,一個鍵盤跳出來總是把輸入框擋住,所以需要移動屏幕來匹配鍵盤.
3 解決虛擬鍵盤擋住UITextField的方法
- (void)keyboardWillShow:(NSNotification *)noti
{
//鍵盤輸入的界面調整
//鍵盤的高度
float height = 216.0;
CGRect frame = self.view.frame;
frame.size = CGSizeMake(frame.size.width,frame.size.height - height);
[UIView beginAnimations:@"Curl"context:nil];
//動畫開始
[UIView setAnimationDuration:0.30];
[UIView setAnimationDelegate:self];
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width,self.view.frame.size.height);
//CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
self.view.frame = rect;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
//鍵盤高度216
int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
if (offset > 0) {
CGRect rect = CGRectMake(0.0f, -offset,width,height);
self.view.frame = rect;
}
[UIView commitAnimations];
}
只要在代碼中加入這三個文件,然后設置自身delegate就可以實現屏幕的移動,但是這里經常會有屏幕移動后不能返回的問題,這里的解決方案就是
- (IBAction)backgroundTap:(id)sender
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width,self.view.frame.size.height);
self.view.frame = rect;
}
在backgroundTap函數中添加這些代碼,這樣屏幕就會返回正常了。
總結
以上是生活随笔為你收集整理的uitextfield 键盘类型_UITextField 键盘弹出问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 输入法画面_搜狗输入法去广告版,流畅再无
- 下一篇: 如何linux查看mysql目录下日志_