在自定義鍵盤的時候,碰到要根據(jù)橫豎屏,然后改變自定義鍵盤高度的需求,但是發(fā)現(xiàn),無論使用autolayout還是設置frame去改變高度,都沒有反應。后面經(jīng)過查閱,才知道有個Intrinsic Content Size屬性??梢栽O置視圖的自定義大小。
創(chuàng)新互聯(lián)是一家企業(yè)級云計算解決方案提供商,超15年IDC數(shù)據(jù)中心運營經(jīng)驗。主營GPU顯卡服務器,站群服務器,綿陽機房托管,海外高防服務器,成都機柜租用,動態(tài)撥號VPS,海外云手機,海外云服務器,海外服務器租用托管等。
關于 intrinsicContentSize 及 約束優(yōu)先級/content Hugging/content Compression Resistance的詳解,參考如下博客:
下面是自己的簡單記錄:
改變自定義鍵盤的高度可以設置鍵盤View的視圖Intrinsic Content Size屬性。
先設置屬性:
然后再調(diào)用方法:
大概demo如下:
一、鍵盤類型
UIKit框架支持8種風格鍵盤。
typedef enum {
UIKeyboardTypeDefault, // 默認鍵盤:支持所有字符
UIKeyboardTypeASCIICapable, // 支持ASCII的默認鍵盤
UIKeyboardTypeNumbersAndPunctuation, // 標準電話鍵盤,支持+*#等符號
UIKeyboardTypeURL, // URL鍵盤,有.com按鈕;只支持URL字符
UIKeyboardTypeNumberPad, //數(shù)字鍵盤
UIKeyboardTypePhonePad, // 電話鍵盤
UIKeyboardTypeNamePhonePad, // 電話鍵盤,也支持輸入人名字
UIKeyboardTypeEmailAddress, // 用于輸入電子郵件地址的鍵盤
} UIKeyboardType;
用法用例:
textView.keyboardtype= UIKeyboardTypeNumberPad;
UIKeyboardTypeDefault:常用于文本輸入
UIKeyboardTypeASCIICapable:常用于密碼輸入
UIKeyboardTypeNumbersAndPunctuation:和上一個鍵盤互相切換
UIKeyboardTypeURL:適用于網(wǎng)址輸入
UIKeyboardTypeNumberPad:只有數(shù)字的數(shù)字鍵盤
UIKeyboardTypePhonePad:可用于撥號的數(shù)字鍵盤,帶*#+
UIKeyboardTypeNamePhonePad:字母及數(shù)字鍵盤
UIKeyboardTypeEmailAddress:適用于郵件地址輸入的鍵盤
UIKeyboardTypeDecimalPad:帶“點”的數(shù)字鍵盤,可用于帶有小數(shù)點的數(shù)字輸入
UIKeyboardTypeTwitter
UIKeyboardTypeWebSearch:適用于網(wǎng)頁搜索的鍵盤
UIKeyboardTypeAlphabet
二 鍵盤高度
以下幾種鍵盤類型幾乎一樣,鍵盤高度也是一樣的
UIKeyboardTypeAlphabet
UIKeyboardTypeASCIICapable
UIKeyboardTypeDefault
UIKeyboardTypeEmailAddress
UIKeyboardTypeNamePhonePad
UIKeyboardTypeNumbersAndPunctuation(數(shù)字和標點符號)
UIKeyboardTypeTwitter
UIKeyboardTypeURL
UIKeyboardTypeWebSearch
5.5吋271
4.7吋258
4.0吋253
②以下幾種鍵盤為數(shù)字類型的鍵盤,鍵盤高度也是一樣的
UIKeyboardTypeDecimalPad(帶小數(shù)點的數(shù)字鍵盤)
UIKeyboardTypeNumberPad(純數(shù)字鍵盤)
UIKeyboardTypePhonePad(帶*+#,;的數(shù)字鍵盤)
5.5吋226
4.7吋216
4.0吋216
鏈接:
鏈接:
在ios開發(fā)中,鍵盤很常用。在sdk版本5.0以前,鍵盤高度是固定值216px;5.0出來以后,鍵盤高度會隨著鍵盤語言變化(中文要高些),在這種情況下一般而言對于界面需要重新布局。方法是利用NSNotificationCenter。
UIKeyboardWillShowNotification;UIKeyboardDidShowNotification; UIKeyboardWillHideNotification; UIKeyboardDidHideNotification;
這幾個notification是5.0sdk之前就有的,顧名思義就知道意思了。
UIKeyboardWillChangeFrameNotification __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);UIKeyboardDidChangeFrameNotification __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
這兩個是sdk 5.0以后出來的,用來處理鍵盤高度的變化。
使用方法是:首先在notification注冊觀察者,比如:
if([[[UIDevice currentDevice] systemVersion] floatValue] = 5.0) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];}
當鍵盤高度將要變化時,就會收到通知,在通知的參數(shù)中可以得到鍵盤目前的高度和變化的目標高度,比如:
-(void)keyboardWillChangeFrame:(NSNotification*)notif{#if __IPHONE_OS_VERSION_MIN_REQUIRED = __IPHONE_3_2 NSValue *keyboardBoundsValue = [[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; #else NSValue *keyboardBoundsValue = [[notif userInfo] objectForKey:UIKeyboardBoundsUserInfoKey]; #endif CGRect keyboardEndRect = [keyboardBoundsValue CGRectValue]; CGRect inputFrame = self.feedBackTextView.frame; //kb 216 vs textFrame 185 float delta = keyboardEndRect.size.height - 216; float originalHeight = inputFrame.size.height; inputFrame.size.height = 185 - delta; if (inputFrame.size.height != originalHeight) { self.feedBackTextView.frame = inputFrame; self.feedBackBackgroundView.frame = inputFrame; }}
另外一些從notification.userInfo中可以取得的key如下:
UIKeyboardFrameBeginUserInfoKey // NSValue of CGRectUIKeyboardFrameEndUserInfoKey // NSValue of CGRectUIKeyboardAnimationDurationUserInfoKey // NSNumber of doubleUIKeyboardAnimationCurveUserInfoKey // NSNumber of double
notif中userInfo的完整信息如下 :
keyboardChange:{ UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";}
下面是一個完整的解決方案,用戶需要知道鍵盤高度的細致變化
下面這個解決方案就只考慮鍵盤出現(xiàn)和消失的處理
當前文章:ios開發(fā)鍵盤高度,ios獲取鍵盤高度
分享鏈接:http://sd-ha.com/article16/phhjdg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、網(wǎng)頁設計公司、網(wǎng)站策劃、虛擬主機、關鍵詞優(yōu)化、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)