




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、 iPhone鍵盤通知與鍵盤定制 藍(lán)科教育學(xué)習(xí)經(jīng)驗(yàn)一.鍵盤通知當(dāng)文本View(如UITextField,UITextView,UIWebView內(nèi)的輸入框)進(jìn)入編輯模式成為first responder時(shí),系統(tǒng)會(huì)自動(dòng)顯示鍵盤。成為firstresponder可能由用戶點(diǎn)擊觸發(fā),也可向文本View發(fā)送becomeFirstResponder消息觸發(fā)。當(dāng)文本視圖退出first responder時(shí),鍵盤會(huì)消失。文本View退出first responder可能由用戶點(diǎn)擊鍵盤上的Done或Return鍵結(jié)束輸入觸發(fā),也可向文本View發(fā)送resignFirstResponder消息觸發(fā)。當(dāng)鍵盤顯示或
2、消失時(shí),系統(tǒng)會(huì)發(fā)送相關(guān)的通知:UIKeyboardWillShowNotificationUIKeyboardDidShowNotificationUIKeyboardWillHideNotificationUIKeyboardDidHideNotification通知消息NSNotification中的userInfo字典中包含鍵盤的位置和大小信息,對應(yīng)的key為UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKeyUIKeyboardAnimationDurationUserInfoKeyUIKeyboardAnimatio
3、nCurveUserInfoKeyUIKeyboardFrameBeginUserInfoKey,UIKeyboardFrameEndUserInfoKey對應(yīng)的Value是個(gè)NSValue對象,內(nèi)部包含CGRect結(jié)構(gòu),分別為鍵盤起始時(shí)和終止時(shí)的位置信息。UIKeyboardAnimationCurveUserInfoKey對應(yīng)的Value是NSNumber對象,內(nèi)部為UIViewAnimationCurve類型的數(shù)據(jù),表示鍵盤顯示或消失的動(dòng)畫類型。UIKeyboardAnimationDurationUserInfoKey對應(yīng)的Value也是NSNumber對象,內(nèi)部為double類型的數(shù)
4、據(jù),表示鍵盤h顯示或消失時(shí)動(dòng)畫的持續(xù)時(shí)間。例如,在UIKeyboardWillShowNotification,UIKeyboardDidShowNotification通知中的userInfo內(nèi)容為userInfo = UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25" UIKeyboardBoundsUserInfoKey = "NSRect: 0, 0, 320, 216" UIKeyboardCenterBeginUser
5、InfoKey = "NSPoint: 160, 588" UIKeyboardCenterEndUserInfoKey = "NSPoint: 160, 372" UIKeyboardFrameBeginUserInfoKey = "NSRect: 0, 480, 320, 216" UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: 0, 264, 320, 216"在UIKeyboardWi
6、llHideNotification,UIKeyboardDidHideNotification通知中的userInfo內(nèi)容為:userInfo = UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25" UIKeyboardBoundsUserInfoKey = "NSRect: 0, 0, 320, 216" UIKeyboardCenterBeginUserInfoKey = "NSPoint: 160, 372&qu
7、ot; UIKeyboardCenterEndUserInfoKey = "NSPoint: 160, 588" UIKeyboardFrameBeginUserInfoKey = "NSRect: 0, 264, 320, 216" UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: 0, 480, 320, 216"在Text,Web, and Editing Programming Guide for iOS中
8、,有如下描述Note: The rectangle contained in the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey properties of the userInfo dictionary should be used only for the size information it contains. Do not use the origin of the rectangle (which is always 0.0, 0.0) in rectangle-intersection ope
9、rations. Because the keyboard is animated into position, the actual bounding rectangle of the keyboard changes over time.但從實(shí)際獲取的信息來看,矩形的origin并不為0.0, 0.0,這里應(yīng)該不準(zhǔn)確。二.文本對象與WebView鍵盤設(shè)置UITextFiled和UITextView都遵循UITextInputTraits協(xié)議,在UITextInputTraits協(xié)議中定義了設(shè)置鍵盤的屬性,有1. keyboardType:鍵盤類型,如UIKeyboardType
10、Default,UIKeyboardTypeURL,UIKeyboardTypeEmailAddress,UIKeyboardTypePhonePad等2. returnKeyType:鍵盤Return鍵顯示的文本,默認(rèn)為”Return”,其他可選擇的包括Go,Next,Done,Send,Google等。3. keyboardAppearance:鍵盤外觀,默認(rèn)為UIKeyboardAppearanceDefault,即上圖中的淺蘭色不透明背景,另外還有一種為UIKeyboardAppearanceAlert,鍵盤背景為黑色半透明,用于在警告框輸入時(shí)顯示,例如appSto
11、re中輸入密碼時(shí):若想顯示黑色鍵盤又不想透明露出底部視圖,可以將鍵盤配置成Alert類型的,然后監(jiān)聽鍵盤顯示的廣播通知,在顯示鍵盤時(shí)在鍵盤底部增加一不透明黑色背景視圖。注:在蘋果的鍵盤示例程序KeyboardAccessory中,將UITextView鍵盤類型更改為UIKeyboardAppearanceAlert,在iPad模擬器中運(yùn)行鍵盤并沒有出現(xiàn)黑色透明的效果,不知為何? 在iPhone中UIKeyboardAppearanceAlert設(shè)置有效。4.autocapitalizationType:文本大小寫樣式,見UITextAutocapitalizationType。5.autoco
12、rrectionType:是否自動(dòng)更正,見UITextAutocorrectionType。6.spellCheckingType:拼寫檢查設(shè)置,見UITextSpellCheckingType。7.enablesReturnKeyAutomatically:是否在無文本時(shí)禁用Return鍵,默認(rèn)為NO。若為YES,則用戶至少輸入一個(gè)字符后Return鍵才被激活。 8.secureTextEntry:若輸入的是密碼,可設(shè)置此類型為YES,輸入字符時(shí)可顯示最后一個(gè)字符,其他字符顯示為點(diǎn)。UIWebView本身不直接遵循UITextInputTraits協(xié)議,但同樣可設(shè)置其內(nèi)部輸入部件的
13、鍵盤屬性。如Configuring the Keyboard for Web Views中所述。設(shè)置autocorrect, auto-capitalization屬性。<input type="text" size="30" autocorrect="off" autocapitalization="on">設(shè)置鍵盤類型:Text: <input type="text"></input>Telephone: <input type="tel&
14、quot;></input>URL: <input type="url"></input>Email: <input type="email"></input>Zip code: <input type="text" pattern="0-9*"></input>三. 使用鍵盤通知調(diào)整文本視圖位置當(dāng)文本視圖成為First Responser時(shí)在窗口底部會(huì)顯示出鍵盤,顯示的鍵盤很可能會(huì)將文本視圖蓋住從而無法看到編輯的效果。鍵盤通
15、知的一大用途即在鍵盤顯示或隱藏時(shí)獲取到鍵盤的位置信息,從而可以調(diào)整窗口中的文本視圖位置或大小,使其可以在鍵盤上方顯示。Text, Web, and Editing Programming Guide for iOS中的MovingContent That Is Located Under the Keyboard節(jié)在鍵盤顯示和消失通知中,通過調(diào)整內(nèi)容UIScrollView視圖的contentInset和contentOffset來保證編輯的文本視圖不會(huì)被鍵盤蓋住。其流程為1.在初始化(viewDidLoad或viewWillAppear)時(shí),注冊處理鍵盤通知的方法。- (void)regis
16、terForKeyboardNotifications NSNotificationCenter defaultCenter addObserver:self selector:selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil; NSNotificationCenter defaultCenter addObserver:self selector:selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:
17、nil;2.在鍵盤顯示的通知事件處理中獲取到即將顯示鍵盤的大小,將UIScrollView的contentInset設(shè)置為鍵盤的frame區(qū)域,同樣設(shè)置scrollIndicatorInsets保證滾動(dòng)條不會(huì)被鍵盤蓋住。獲取到編輯文本視圖的原點(diǎn)位置,與鍵盤顯示區(qū)域比較,若會(huì)被鍵盤覆蓋,則調(diào)整contentOffset以使其在鍵盤上方/ Called when the UIKeyboardDidShowNotification is sent.- (void)keyboardWasShown:(NSNotification*)aNotification NSDictionary* info =
18、aNotification userInfo; CGSize kbSize = info objectForKey:UIKeyboardFrameBeginUserInfoKey CGRectValue.size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; / If active text field i
19、s hidden by keyboard, scroll it so it's visible / Your application might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin
20、.y-kbSize.height); scrollView setContentOffset:scrollPoint animated:YES; 3.在鍵盤消失的通知處理事件中,簡單的將UIScrollView恢復(fù)即可/ Called when the UIKeyboardWillHideNotification is sent- (void)keyboardWillBeHidden:(NSNotification*)aNotification UIEdgeInsets contentInsets = UIEdgeInsetsZero; scrollView.contentInset = co
21、ntentInsets; scrollView.scrollIndicatorInsets = contentInsets;在蘋果的KeyboardAccessory示例程序中同樣演示了使用鍵盤通知來調(diào)整文本視圖位置的代碼,其中使用了鍵盤通知中的鍵盤動(dòng)畫時(shí)間,從而使文本視圖移動(dòng)的動(dòng)畫與鍵盤的顯示和消失同步。- (void)keyboardWillShow:(NSNotification *)notification /* Reduce the size of the text view so that it's not obscured by the keyboard. Animate
22、 the resize so that it's in sync with the appearance of the keyboard. */ NSDictionary *userInfo = notification userInfo; / Get the origin of the keyboard when it's displayed. NSValue* aValue = userInfo objectForKey:UIKeyboardFrameEndUserInfoKey; / Get the top of the keyboard as the y coordin
23、ate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. CGRect keyboardRect = aValue CGRectValue; keyboardRect = self.view convertRect:keyboardRect fromView:nil; CGFloat keyboardTop = keyboa
24、rdRect.origin.y; CGRect newTextViewFrame = self.view.bounds; newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y; / Get the duration of the animation. NSValue *animationDurationValue = userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey; NSTimeInterval animationDuration;
25、animationDurationValue getValue:&animationDuration; / Animate the resize of the text view's frame in sync with the keyboard's appearance. UIView beginAnimations:nil context:NULL; UIView setAnimationDuration:animationDuration; textView.frame = newTextViewFrame; UIView commitAnimations;- (
26、void)keyboardWillHide:(NSNotification *)notification NSDictionary* userInfo = notification userInfo; /* Restore the size of the text view (fill self's view). Animate the resize so that it's in sync with the disappearance of the keyboard. */ NSValue *animationDurationValue = userInfo objectFo
27、rKey:UIKeyboardAnimationDurationUserInfoKey; NSTimeInterval animationDuration; animationDurationValue getValue:&animationDuration; UIView beginAnimations:nil context:NULL; UIView setAnimationDuration:animationDuration; textView.frame = self.view.bounds; UIView commitAnimations;關(guān)于調(diào)整文本視圖大小的代碼,還可以參
28、考Different way to show keyboard and resize UIView with text field。還可以在鍵盤通知中實(shí)現(xiàn)自定義鍵盤,例如UIKEYBOARDTYPENUMBERPAD AND THE MISSING “RETURN” KEY中使用鍵盤通知在數(shù)字鍵盤中增加Return鍵,還可參考UIKeyboardTypeNumberPad鍵盤增加Return鍵注:若在同一窗口中有兩個(gè)UITextField,當(dāng)?shù)谝粋€(gè)UITextField成為First Responser時(shí),開始顯示鍵盤并接收到UIKeyboardWillShowNotification,UIK
29、eyboardDidShowNotification通知,當(dāng)First Responser轉(zhuǎn)移到第二個(gè)UITextField時(shí),鍵盤會(huì)一直顯示,此時(shí)不會(huì)收到WillShow和DidShow的通知。四.使用inputAccessoryView與inputView定制輸入視圖inputAccessoryView和inputView屬性在UIResponder中定義,為readonly的屬性,但在UITextFiled和UITextView中重新定義為了readwrite的屬性,可以由用戶賦值。若inputView的不為nil,則當(dāng)文本視圖成為first responder時(shí),不會(huì)顯示系統(tǒng)鍵盤,而是顯示自定義的inputView;若inputAccessoryView不為nil,則inputAccessoryView會(huì)顯示在系統(tǒng)鍵盤或定制inputView的上方。當(dāng)使用inputView時(shí),仍然會(huì)有WillShow,DidShow,WillHide,DidHide的鍵盤通知,通知中的BeginFrame與EndFrame為系統(tǒng)鍵盤(或inputView)與inputAccessoryView一起的frame。 蘋果鍵盤示例程序KeyboardAccessory演示了inputAccessoryView的使用方法。
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T-ZLX 088-2024 綠色食品 永嘉早香柚生產(chǎn)技術(shù)規(guī)程
- 二零二五年度新材料研發(fā)股份分紅及市場拓展合同模板
- T-ZGZX 0003-2024 成年智力殘疾人托養(yǎng)服務(wù)指南
- 二零二五年度夫妻共同財(cái)產(chǎn)保全與婚后生活規(guī)劃協(xié)議
- 二零二五年度企業(yè)合同管理制度與品牌建設(shè)合同
- 二零二五年度智慧城市建設(shè)抵押貸款協(xié)議
- 二零二五年度城市建筑工地渣土車租賃管理協(xié)議
- 二零二五年度農(nóng)村土地承包經(jīng)營權(quán)流轉(zhuǎn)與農(nóng)業(yè)病蟲害防治服務(wù)合同
- 二零二五年度高科技企業(yè)股權(quán)合作協(xié)議書
- 2025年度生物制藥產(chǎn)業(yè)合作投資合同
- 《讓我們的家更美好》教學(xué)設(shè)計(jì)
- 自抗擾控制器及其應(yīng)用
- 熱工與流體力學(xué)基礎(chǔ)課件
- 石膏外固定的健康教育
- LOI意向書中英文模板
- 《防毒面具的使用》課件
- 《金融風(fēng)險(xiǎn)管理》 課程標(biāo)準(zhǔn)
- 銷售費(fèi)用管理制度
- 高維數(shù)據(jù)分析
- 成人癌性疼痛護(hù)理指南解讀
- 小學(xué)語文 一年級第三單元【集體備課】
評論
0/150
提交評論