iOS 切換鍵盤

來源:互聯網
上載者:User

標籤:

1.點擊toolbar的加號按鈕切換鍵盤

       切換前                 切換後

      

 

2 要實現上述效果2.1 需要監聽鍵盤的彈出\隱藏

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

 1 /** 2  *  鍵盤即將隱藏 3  */ 4 - (void)keyboardWillHide:(NSNotification *)note 5 { 6     // 1.鍵盤彈出需要的時間 7     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 8     // 2.動畫 9     [UIView animateWithDuration:duration animations:^{10         self.toolbar.transform = CGAffineTransformIdentity;11     }];12 }
 1 /** 2  *  鍵盤即將彈出 3  */ 4 - (void)keyboardWillShow:(NSNotification *)note 5 { 6     // 1.鍵盤彈出需要的時間 7     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 8     // 2.動畫 9     [UIView animateWithDuration:duration animations:^{10         // 取出鍵盤高度11         CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];12         CGFloat keyboardH = keyboardF.size.height;13         self.toolbar.transform = CGAffineTransformMakeTranslation(0, - keyboardH);14     }];15 }
2.2 toolbar的懶載入方法
 1 - (UIToolbar *)toolbar{ 2     if (_toolbar == nil) { 3         _toolbar = [[UIToolbar alloc]init]; 4         _toolbar.frame = CGRectMake(0, screenHeight - kToolbarHeight , screenWidth, kToolbarHeight); 5         [self.view addSubview:_toolbar]; 6          7         // 切換鍵盤的按鈕 8         UIButton *changeKeybord = [UIButton buttonWithType:UIButtonTypeContactAdd]; 9         [_toolbar addSubview:changeKeybord];10 11         [changeKeybord addTarget:self action:@selector(changeKeybord) forControlEvents:UIControlEventTouchDown];12     }13     return _toolbar;14 }
2.3 實現changeKeybord方法
 1 - (void)changeKeybord 2 { 3     if (self.mytext.inputView) { //inputView存在則說明是自訂鍵盤 4         self.mytext.inputView = nil; //把自訂鍵盤清空,則就變回系統鍵盤 5     }else{ //反之,inputView不存在,則說明當前顯示的是系統鍵盤 6         UIView *newKeybord = [[UIView alloc]init];  // 執行個體化一個新鍵盤 7         newKeybord.bounds = CGRectMake(0, 0, 320, 216); 8         newKeybord.backgroundColor = [UIColor yellowColor]; 9         self.mytext.inputView = newKeybord; //將新鍵盤賦值給inputView10     }11 12     // 下面這句不能省,因為切換鍵盤後,只有先隱藏再彈出,才會調出新鍵盤13     [self.mytext resignFirstResponder];14     // 為了讓鍵盤切換互動效果更順暢,增加延遲,如果不增加延遲,會出現:舊鍵盤還沒完全隱藏,新鍵盤就已經彈出了15     dispatch_after((uint64_t)3.0, dispatch_get_main_queue(), ^{16         [self.mytext becomeFirstResponder];17     });18     19 }
2.4 自訂一個新鍵盤

未完待續

iOS 切換鍵盤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.