成都创新互联网站制作重庆分公司

ios开发键盘,iOS开发键盘不跟着页面消失而消失

iOS开发中的键盘高度变化处理

在ios开发中,键盘很常用。在sdk版本5.0以前,键盘高度是固定值216px;5.0出来以后,键盘高度会随着键盘语言变化(中文要高些),在这种情况下一般而言对于界面需要重新布局。方法是利用NSNotificationCenter。

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于做网站、成都做网站、凉山州网络推广、重庆小程序开发公司、凉山州网络营销、凉山州企业策划、凉山州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供凉山州建站搭建服务,24小时服务热线:18982081108,官方网址: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];}

当键盘高度将要变化时,就会收到通知,在通知的参数中可以得到键盘目前的高度和变化的目标高度,比如:

-(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}}";}

下面是一个完整的解决方案,用户需要知道键盘高度的细致变化

下面这个解决方案就只考虑键盘出现和消失的处理

怎么使iOS中的键盘适应高度变化

在ios开发时我们会遇到键盘高度无法适应的问题,这时候该怎么解决呢?下面由我教大家怎么解决iOS中的键盘高度变化的问题。

    完美解决iOS中的键盘适应高度变化的 方法

#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;

}];

}

通过获取键盘消息的开始状态、结束状态,以及变化周期,可以计算出具体的Y偏移,从而在相同时间里做相同偏移量。

猜你喜欢:

2. 怎样把电脑上的照片导入iphone

3. iphone照片怎么导入电脑

4. 电脑ipad模拟器的安装方法

5. 安卓程序员必备的开发工具

6. iPhone5s怎么刷机

iOS提供的11种键盘

iOS系统提供了11种键盘类型,供开发者们根据情境选用。

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

iOS开发 iPad键盘撤销操作的注意点

键盘的撤销按键是iPad独有的一个按键,在手机端是不存在这个按键的,但是手机端同样也存在撤销操作,只不过并不是通过键盘上的按键来进行撤销操作的。

当对 TextView 或者是 TextField 添加了限制输入长度。然后在控件中输入到最长长度,这时候继续去输入东西,但是从界面上来看我们输入的东西是没有显示出来的。此时按一下键盘上的撤销按键

TextView

TextField

具体的内部细节问题还是要大家共同去探索学习,这里只提供了一下解决方案

当我们在输入键盘自带的表情的时候一定是要去注意一下的。因为他的长度是 2 ,我们正常输入数字、字母、汉字等等可以看做是 1 ,所以如果输入框里面带有表情一定要注意判断长度。

检测是否有表情


文章名称:ios开发键盘,iOS开发键盘不跟着页面消失而消失
文章分享:http://cxhlcq.com/article/phpjdp.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部