UITextField一些技巧總結

咱們在項目中確定要用到UITextField容許用戶輸入,好比在登陸、註冊界面。小小的TextField有必要專門出一篇教程嗎?固然是有必要的,由於使用場景不一樣,因此就有了不一樣的需求,本篇博客結合本身使用的一些需求,寫出來與你們分享,若是有什麼不對或者不足,請指正;若是你們有更好的方案,但願可以與我分享。另:本篇博客採用xib的編碼方式,在關鍵地方會貼有圖片,由於xib能夠提升寫博客速度,並且這也是ios開發的一個趨勢。
ios


一、需求1:改變TextField背景圖片,app

默認的TextField的Border Style這樣的ide

此時你想設置背景圖片,是沒有效果的。選擇其餘三種Border Style,就能夠設置背景圖片了,很腦殘的例子吧。學習


二、多個TextField時候,點擊軟鍵盤的換行(return)按鈕,FirstResponder的切換編碼

其實,就是將FirstResponder切換到下一個UITextField,到最後一個TextField時候,點擊換行(return)按鈕,鍵盤退出。atom

步驟:spa

首先在ErrorViewController.xib上面拖4個UITextField(聲明下,這裏的ErrorViewController中的Error沒有什麼「錯誤的意思」,就是隨便命名的,各位不要被誤導),從上到下一次擺放,而後右鍵連線TextField至File’s Owner,這是爲了設置delegate,以前的博客我已經說明了設置代理delegate的重要性,這一步每每容易被忽略和遺忘。接着,關聯xib中的四個UITextField到ErrorViewController.h,分別命名爲errorTF一、errorTF二、errorTF三、errorTF4。.net

ErrorViewController.h代碼以下3d

@interface ErrorViewController : UIViewController代理

@property (strong, nonatomic) IBOutlet UITextField *errorTF1;

@property (strong, nonatomic) IBOutlet UITextField *errorTF2;

@property (strong, nonatomic) IBOutlet UITextField *errorTF3;

@property (strong, nonatomic) IBOutlet UITextField *errorTF4;

@end

在ErrorViewController.m中,ErrorViewController實現UITextFieldDelegate協議,而後實現協議中一個optional的方法- (BOOL)textFieldShouldReturn:(UITextField *)textField,ErrorViewController的部分代碼以下,

@interface ErrorViewController ()<UITextFieldDelegate>


@end


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    return NO;//這裏return NO或者YES貌似沒有什麼區別,我很疑惑,但願高手給我解惑。

}

這樣就完成了基本要作的事情,而後是最後一步,就是完善並填充- (BOOL)textFieldShouldReturn:(UITextField *)textField方法,其完整代碼以下,

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    if (textField == errorTF1) {

        [errorTF2 becomeFirstResponder];

    } else if (textField == errorTF2) {

        [errorTF3 becomeFirstResponder];

    } else if (textField == errorTF3) {

        [errorTF4 becomeFirstResponder];

    } else if (textField == errorTF4){

        [errorTF4 resignFirstResponder];

    }

    return NO;//YES or NO,that is a question

}

這段代碼相信你們都看得懂,很少作解釋了,這樣就實現了點擊換行(return)按鈕,firstResponder跳轉到下一個TextField,到最後一個TextField時候,鍵盤退出。



三、需求2:多個TextField,鍵盤遮擋,界面上移,

原本準備寫一下錯誤的代碼,給各位一點提醒不要誤入歧途,可是忘了以前錯誤代碼怎麼寫的,並且再重寫的話也比較浪費時間,因此直接寫出我整理好的正確的方法吧。咱們知道,在iPhone豎屏的狀況下,軟鍵盤的高度是216,因此若是爲了避免讓鍵盤擋住TextField,那麼應該把界面向上移動,讓TextField的底部恰好在鍵盤的頂部,這種狀況下使用NSNotification通知,是可以比較好的解決這個問題。

此次拖動10個UITextField到TFViewController.xib裏面,重複上面「需求2」的步驟,設置delegate,在TFViewController.h中聲明10個UITextField property,分別命名爲tf一、tf二、tf三、……、tf10,TFViewController實現UITextFieldDelegate協議,並實現其中方法-(BOOL)textFieldShouldReturn:(UITextField *)textField可選(optional)方法,以下所示,

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    if (textField == tf1) {

        [tf2 becomeFirstResponder];

    } else if (textField == tf2)

    {

        [tf3 becomeFirstResponder];

    } else if (textField == tf3)

    {

        [tf4 becomeFirstResponder];

    } else if (textField == tf4)

    {

        [tf5 becomeFirstResponder];

    } else if (textField == tf5)

    {

        [tf6 becomeFirstResponder];

    } else if (textField == tf6)

    {

        [tf7 becomeFirstResponder];

    } else if (textField == tf7)

    {

        [tf8 becomeFirstResponder];

    } else if (textField == tf8)

    {

        [tf9 becomeFirstResponder];

    } else if (textField == tf9) {

        [tf10 becomeFirstResponder];

    }  else if (textField == tf10) {

        [tf10 resignFirstResponder];

    }

    return YES;

}

這幾個步驟和「需求2」沒有區別,之因此重複一遍,是由於這幾個步驟是「需求3」實現的基礎,因此寫一遍也不嫌多。

接下來就是實現界面上移,不遮擋TextField的關鍵地方了。很幸運的是,蘋果在系統給咱們封裝了「鍵盤彈出」和「鍵盤消失」的事件,此處咱們只須要用到「鍵盤消失」的系統通知,咱們可使用通知來監聽鍵盤的行爲,判斷當前爲firstResponder的TextField是否被鍵盤遮擋,若是被遮擋,那麼屏幕上移,不然不作處理,這是很簡單的邏輯,實現的代碼以下,




(2)在TFViewController的- (void)viewWillAppear裏面添加監聽「鍵盤消失」的通知,

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWillHide:)

name:UIKeyboardWillHideNotification object:nil];

    

}

相應的,咱們須要在- (void)viewWillDisappear時候,將NSNotification移除,這就像內存管理同樣,你在不用的時候須要移除你添加的通知,

鍵盤彈出和消失的系統事件就是UIKeyboardWillShowNotificationUIKeyboardWillHideNotification針對「鍵盤消失」的狀況keyboardWillHide來處理,固然這些代碼是寫在TFViewController.m文件中的,下面就是實現這該方法的代碼,

- (void)keyboardWillHide:(NSNotification *)notification

{

    [UIView animateWithDuration:0.3 animations:^{

        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    }];

    

}

這是將self.view移回到原來位置的方法,那麼對應的將界面上移的代碼是寫在什麼地方的呢?就是寫在某個TextField變爲firstResponder、處於被編輯的時候,咱們進行判斷並看是否要上移self,view,代碼以下,

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    [UIView animateWithDuration:0.3 animations:^(void) {

        CGFloat keyboardHeight = 216;//鍵盤高度

        //textField底部的座標

        CGFloat bottomY = textField.frame.origin.y+textField.frame.size.height;

        //VIEW底部到textField底部的距離

        CGFloat tempHeight = self.view.frame.size.height-bottomY;

        //鍵盤高度超過BUtton底部的距離,即須要viewframe上提得高度

        CGFloat upOffset = keyboardHeight-tempHeight;

        if (upOffset>0) {

            [UIView animateWithDuration:0.3 animations:^{

                self.view.frame = CGRectMake(0, -upOffset, self.view.frame.size.width, self.view.frame.size.height);

            }];

        }

    }];

}

這樣鍵盤就不會擋住TextField,不會讓用戶不能輸入的。

—————————————————————————————————————————————

各位有沒有以爲這樣實現,代碼寫的不「對稱」,由於UIKeyboardWillHideNotificationUIKeyboardWillShowNotification一塊兒出現才感受好,還有就是代理方法- (void)textFieldDidBeginEditing- (void)textFieldDidEndEditing一塊兒出現才完美,實際狀況是我也想這樣纔好,可是沒用到就沒用到了唄,有強迫症的觀衆不要抓狂啊。其實我也試過了,若是隻使用UIKeyboardWillHideNotificationUIKeyboardWillShowNotification不能實現想要的結果,若是隻使用textFieldDidEndEditingtextFieldDidBeginEditing一樣一不行,我這樣交叉着各使用了一個,剛好實現了。若是有人能寫出「對稱」的代碼,來實現這個功能,請讓我也知道。



本人建了一個ios交流羣188647173,但願你們能在羣裏相互學習,共同進步。求大神照耀啊。。。

個人郵箱是zheniyerenrou@163.com,有什麼質疑或者疑問,能夠發我郵件。

相關文章
相關標籤/搜索