iOS 鍵盤擋住UITextField

來源:互聯網
上載者:User

標籤:ios   虛擬鍵盤   uitextfield   ipad   iphone   

iOS常用的兩個功能:點擊螢幕和return隱藏虛擬鍵盤和解決虛擬鍵盤擋住UITextField的方法

iOS上面對鍵盤的處理很不人性化,所以這些功能都需要自己來實現, 首先是點擊return和螢幕隱藏鍵盤

這個首先引用雙子座的部落格 http://my.oschina.net/plumsoft/blog/42545,他的文章寫的很好,對大家的理解很有好處。

在 iOS 程式中當想要在文字框中輸入資料,輕觸文字框會開啟鍵盤。對於 iPad 程式,其鍵盤有一個按鈕可以用來關閉鍵盤,但是 iPhone 程式中的鍵盤卻沒有這樣的按鈕,不過我們可以採取一些方法關閉它。例如,我們可以實現按下 Rerun (有時也是 Done、Research 等)鍵關閉鍵盤,或者,更人性化的,輕觸背景關閉鍵盤。

1、首先講一下按下Return鍵關閉鍵盤。

當按下鍵盤的 Return 鍵,會產生一個 Did End On Exit 事件,此時,我們告訴文字框要放棄控制項,於是鍵盤就消失了。

假設,我們已經建立了一個 Single View Application ,並開啟 ViewController.xib 檔案,在 View 上拖上去了三個 Text Field ,然後,我們把這三個文字框映射到 ViewController.h 中,名稱依次是 firstField、secondField 以及 thirdField 。如:

在這個基礎上,實現輕觸 Return 關閉鍵盤,步驟為:

(1)在 ViewController.h 中聲明一個方法:

- (IBAction)textFiledReturnEditing:(id)sender;

 

(2)在 ViewController.m 中實現這個方法:

-(IBAction)textFiledReturnEditing:(id)sender {    [sender resignFirstResponder];}

 

所謂 First Responder 指的就是使用者當前正在與之互動的控制項。當使用者使用鍵盤時,First Responder 就是這個鍵盤,resignFirstResponder 方法,顧名思義,就是放棄 First Responder 。

(3)讓這三個文字框都映射到 textFiledReturnEditing 方法,不過此時的事件應當是 Did End On Exit ,具體操作是:

開啟 Assistant Editor  ,左邊開啟 ViewController.xib ,右邊開啟 ViewController.h ,在 Xcode 最右邊開啟 Connector Inspector ,然後在 View 中選擇第一個文字框,在 Connector Inspector 中找到 Did End On Exit ,從它右邊的圓圈中拉出映射線,映射到 ViewController.h 的 textFiledReturnEditing 方法,如:

給其他兩個文字框進行同樣的操作。現在,已經實現了輕觸 Return 鍵關閉鍵盤。

2、下面介紹更人性化的方法,輕觸背景關閉鍵盤。

跟上面的步驟差不多,首先定義一個方法,然後實現這個方法,接下來將指定的控制項映射到這個方法,並選擇好所觸發的事件。不同的是,這次我們要選擇的控制項不是上邊的文字框,而是視圖 View 本身。

(1)在 ViewController.h 檔案中添加方法聲明代碼:

- (IBAction)backgroundTap:(id)sender;

 

(2)在ViewController.m中實現這個方法:

- (IBAction)backgroundTap:(id)sender {    [firstField resignFirstResponder];    [secondField resignFirstResponder];    [thirdField resignFirstResponder];}

 

需要說明的是,[firstField resignFirstResponder];表示,如果firstField有FirstResponder的話就放棄它,我們不用先判斷firstField是否有,這條語句完全正確。

(3)讓 View 映射到這個方法,不過事先,我們先要改變 View 的類型。

開啟xib,選中 View ,開啟 Identity Inspector ,在 class 中選擇 UIControl :

(4)開啟Assistant Editor ,左邊開啟 ViewController.xib ,右邊開啟 ViewController.h ,在Xcode最右邊開啟 Connector Inspector ,在 ViewController.xib 中選擇 Control ,在 Connector Inspector 中找到 Touch Down ,從它右邊的圓圈中拉出映射線,映射到 ViewController.h 的 backgroundTap 方法,如:

好了,可以運行下看看效果了:

      

 

開啟鍵盤之後,在背景地區點擊一下,鍵盤就會向下收合來。

 

然後點評,在網上也有唯寫一個 backgroundTap 函數,然後將所有組件都 resignFirstResponser的方法,即 將組件的事件和螢幕的事件指向同一個函數。

這兩個方法都是可以用的,但是呢,我更加傾向於使用同一個函數的方法,原因呢,原因就要牽扯到第二個方面的知識:

解決虛擬鍵盤擋住UITextField的方法

因為螢幕太小的緣故,一個鍵盤跳出來總是把輸入框擋住,所以需要移動螢幕來匹配鍵盤

 

#pragma mark -

#pragma mark 解決虛擬鍵盤擋住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

{       

    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.       

    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;

    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//鍵盤高度216

    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];               

}

#pragma mark -

 只要在代碼中加入這三個檔案,然後將自身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函數中添加這些代碼,這樣螢幕就會返回正常了。

iOS 鍵盤擋住UITextField

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.