你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios7學習之路七(隱藏虛擬鍵盤,解決鍵盤擋住UITextField問題)

ios7學習之路七(隱藏虛擬鍵盤,解決鍵盤擋住UITextField問題)

編輯:IOS開發綜合

再正式開始之前,先來介紹一下IOS的鍵盤類型:

一、鍵盤風格

UIKit框架支持8種風格鍵盤

typedef enum {  
    UIKeyboardTypeDefault,                // 默認鍵盤:支持所有字符   
    UIKeyboardTypeASCIICapable,           // 支持ASCII的默認鍵盤   
    UIKeyboardTypeNumbersAndPunctuation,  // 標准電話鍵盤,支持+*#等符號   
    UIKeyboardTypeURL,                    // URL鍵盤,有.com按鈕;只支持URL字符   
    UIKeyboardTypeNumberPad,              //數字鍵盤   
    UIKeyboardTypePhonePad,               // 電話鍵盤   
    UIKeyboardTypeNamePhonePad,           // 電話鍵盤,也支持輸入人名字   
    UIKeyboardTypeEmailAddress,           // 用於輸入電子郵件地址的鍵盤   
} UIKeyboardType;  


用法用例:

textView.keyboardtype = UIKeyboardTypeNumberPad;

二、鍵盤外觀

  1. typedef enum {  
        UIKeyboardAppearanceDefault,    // 默認外觀:淺灰色   
        UIKeyboardAppearanceAlert,      //深灰/石墨色   
    } UIKeyboardAppearance;  


    用法用例:

    textView.keyboardAppearance=UIKeyboardAppearanceDefault;

    三、回車鍵

    1. typedef enum {  
          UIReturnKeyDefault,  //默認:灰色按鈕,標有Return
          UIReturnKeyGo,  //標有Go的藍色按鈕
          UIReturnKeyGoogle,  //標有Google的藍色按鈕,用於搜索
          UIReturnKeyJoin,  //標有Join的藍色按鈕
          UIReturnKeyNext,  //標有Next的藍色按鈕
          UIReturnKeyRoute,  //標有Route的藍色按鈕
          UIReturnKeySearch,  //標有Search的藍色按鈕
          UIReturnKeySend,  //標有Send的藍色按鈕
          UIReturnKeyYahoo,  //標有Yahoo!的藍色按鈕,用於搜索
          UIReturnKeyDone,  //標有Done的藍色按鈕
          UIReturnKeyEmergencyCall,  //緊急呼叫按鈕
      } UIReturnKeyType;  


      用法用例:

      textView.returnKeyType=UIReturnKeyGo;

      四、自動大寫

      1. typedef enum {  
            UITextAutocapitalizationTypeNone, //不自動大寫   
            UITextAutocapitalizationTypeWords, //單詞首字母大寫   
            UITextAutocapitalizationTypeSentences, //句子首字母大寫   
            UITextAutocapitalizationTypeAllCharacters, //所有字母大寫   
        } UITextAutocapitalizationType;  


        用法用例:

        textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

        五、自動更正

        1. typedef enum {  
              UITextAutocorrectionTypeDefault,//默認   
              UITextAutocorrectionTypeNo,//不自動更正   
              UITextAutocorrectionTypeYes,//自動更正   
          } UITextAutocorrectionType;  


          用法用例:

          textField.autocorrectionType = UITextAutocorrectionTypeYes;

          六、安全文本輸入

          textView.secureTextEntry=YES;

          開啟安全輸入主要是用於密碼或一些私人數據的輸入,此時會禁用自動更正和自此緩存。

          以上內容都可以在 inspector中設置:

          \

          <喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+CtTaIGlPUyCzzNDy1tC1sc/r0qrU2s7Esb6/8tbQyuTI68r9vt2jrMfhtKXOxLG+v/K74bTyv6q8/MXMoaO21NPaIGlQYWQgs8zQ8qOsxuS8/MXM09DSu7j2sLTFpb/J0tTTw8C0udix1bz8xcyjrLWryscgaVBob25lILPM0PLW0LXEvPzFzMi0w7vT0NXi0fm1xLC0xaWjrLK7uf3O0sPHv8nS1LLJyKHSu9Cpt723qLnYsdXL/KGjwP3I56OsztLDx7/J0tTKtc/WsLTPwiBSZXJ1biCjqNPQyrHSssrHIERvbmWholJlc2VhcmNoILXIo6m8/LnYsdW8/MXMo6y78tXfo6y4/MjL0NS7r7XEo6zH4bSlsbO+sLnYsdW8/MXMoaM8L3A+CjxwPgo8c3Ryb25nPjGhosrXz8i9stK7z8KwtM/CUmV0dXJuvPy52LHVvPzFzKGjPC9zdHJvbmc+PC9wPgo8cD4KtbGwtM/CvPzFzLXEIFJldHVybiC8/KOsu+Gy+sn60ru49iBEaWQgRW5kIE9uIEV4aXQgysK8/qOstMvKsaOsztLDx7jmy9/OxLG+v/LSqrfFxvq/2Lz+o6zT2srHvPzFzL7Nz/vKp8HLoaM8L3A+CjxwPgq82cnoo6zO0sPH0tG+rbS0vajBy9K7uPYgU2luZ2xlIFZpZXcgQXBwbGljYXRpb24go6yyorTyv6ogVmlld0NvbnRyb2xsZXIueGliIM7EvP6jrNTaIFZpZXcgyc/Nz8nPyKXBy8j9uPYgVGV4dCBGaWVsZCCjrMi7uvOjrM7Sw8ew0dXiyP249s7Esb6/8tOzyeS1vSBWaWV3Q29udHJvbGxlci5oINbQo6zD+7PG0sC0zsrHIGZpcnN0RmllbGShonNlY29uZEZpZWxkINLUvLAgdGhpcmRGaWVsZCCho8jnz8LNvKO6PC9wPgo8aW1nIHNyYz0="/uploadfile/Collfiles/20140304/20140304081141200.jpg" alt="\">


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

          - (IBAction)textFiledReturnEditing:(id)sender;
          (2)在 ViewController.m 中實現這個方法:
          -(IBAction)textFiledReturnEditing:(id)sender {
              [sender resignFirstResponder];
          }

          讓這三個文本框都映射到 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、下面介紹輕觸背景關閉鍵盤。

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

          - (IBAction)backgroundTap:(id)sender;
          (2)在ViewController.m中實現這個方法:

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

          (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 方法,如下圖:

          運行結果:

          打開鍵盤之後,在背景區域點擊一下,鍵盤就會向下收起來。


          三.解決虛擬鍵盤擋住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

          控制器添加UITextFieldDelegate

          @interface ViewController : UIViewController
          在viewDidLoad中添加:

          - (void)viewDidLoad
          {
              [super viewDidLoad];
          	self.firstTextField.delegate=self;
              
              self.secondTextField.delegate=self;
              self.thirdTextField.delegate=self;
              
          }

          但是這裡經常會有屏幕移動後不能返回的問題,這裡的解決方案就是

          - (IBAction)backgroundTap:(id)sender {
              [self.firstTextField resignFirstResponder];
              [self.secondTextField resignFirstResponder];
              [self.thirdTextField resignFirstResponder];
              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函數中添加這些代碼,這樣屏幕就會返回正常了。
          運行結果:

          \

          例子下載鏈接http://download.csdn.net/detail/superlele123/6986691








  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved