重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
在ios開發(fā)中,鍵盤很常用。在sdk版本5.0以前,鍵盤高度是固定值216px;5.0出來以后,鍵盤高度會隨著鍵盤語言變化(中文要高些),在這種情況下一般而言對于界面需要重新布局。方法是利用NSNotificationCenter。
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、成都做網(wǎng)站、涼山州網(wǎng)絡(luò)推廣、重慶小程序開發(fā)公司、涼山州網(wǎng)絡(luò)營銷、涼山州企業(yè)策劃、涼山州品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供涼山州建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
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];}
當(dāng)鍵盤高度將要變化時,就會收到通知,在通知的參數(shù)中可以得到鍵盤目前的高度和變化的目標(biāo)高度,比如:
-(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}}";}
下面是一個完整的解決方案,用戶需要知道鍵盤高度的細(xì)致變化
下面這個解決方案就只考慮鍵盤出現(xiàn)和消失的處理
在ios開發(fā)時我們會遇到鍵盤高度無法適應(yīng)的問題,這時候該怎么解決呢?下面由我教大家怎么解決iOS中的鍵盤高度變化的問題。
? ? 完美解決iOS中的鍵盤適應(yīng)高度變化的 方法
#pragma mark - reg unreg notification
- (void)regNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)unregNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark - notification handler
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;
CGRect inputFieldRect = self.inputTextField.frame;
CGRect moreBtnRect = self.moreInputTypeBtn.frame;
inputFieldRect.origin.y += yOffset;
moreBtnRect.origin.y += yOffset;
[UIView animateWithDuration:duration animations:^{
self.inputTextField.frame = inputFieldRect;
self.moreInputTypeBtn.frame = moreBtnRect;
}];
}
通過獲取鍵盤消息的開始狀態(tài)、結(jié)束狀態(tài),以及變化周期,可以計算出具體的Y偏移,從而在相同時間里做相同偏移量。
猜你喜歡:
2. 怎樣把電腦上的照片導(dǎo)入iphone
3. iphone照片怎么導(dǎo)入電腦
4. 電腦ipad模擬器的安裝方法
5. 安卓程序員必備的開發(fā)工具
6. iPhone5s怎么刷機
iOS系統(tǒng)提供了11種鍵盤類型,供開發(fā)者們根據(jù)情境選用。
UIkeyboard Type
下面我們來好好看一下究竟是哪11種鍵盤。
1.UIKeyboardTypeDefault
2.UIKeyboardTypeASCIICapable
3.UIKeyboardTypeNumbersAndPunctuation
4.UIKeyboardTypeURL
5.UIKeyboardTypeNumberPad
6.UIKeyboardTypePhonePad?
7.UIKeyboardTypeNamePhonePad
8.UIKeyboardTypeEmailAddress
9.UIKeyboardTypeDecimalPad
10.UIKeyboardTypeTwitter
11.UIKeyboardTypeWebSearch
鍵盤的撤銷按鍵是iPad獨有的一個按鍵,在手機端是不存在這個按鍵的,但是手機端同樣也存在撤銷操作,只不過并不是通過鍵盤上的按鍵來進(jìn)行撤銷操作的。
當(dāng)對 TextView 或者是 TextField 添加了限制輸入長度。然后在控件中輸入到最長長度,這時候繼續(xù)去輸入東西,但是從界面上來看我們輸入的東西是沒有顯示出來的。此時按一下鍵盤上的撤銷按鍵
TextView
TextField
具體的內(nèi)部細(xì)節(jié)問題還是要大家共同去探索學(xué)習(xí),這里只提供了一下解決方案
當(dāng)我們在輸入鍵盤自帶的表情的時候一定是要去注意一下的。因為他的長度是 2 ,我們正常輸入數(shù)字、字母、漢字等等可以看做是 1 ,所以如果輸入框里面帶有表情一定要注意判斷長度。
檢測是否有表情