资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

ios开发键盘高度,ios输入法如何调整键盘高度

iOS改变自定义键盘高度

在自定义键盘的时候,碰到要根据横竖屏,然后改变自定义键盘高度的需求,但是发现,无论使用autolayout还是设置frame去改变高度,都没有反应。后面经过查阅,才知道有个Intrinsic Content Size属性。可以设置视图的自定义大小。

会昌网站建设公司创新互联建站,会昌网站设计制作,有大型网站制作公司丰富经验。已为会昌千余家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的会昌做网站的公司定做!

关于 intrinsicContentSize 及 约束优先级/content Hugging/content Compression Resistance的详解,参考如下博客:

下面是自己的简单记录:

改变自定义键盘的高度可以设置键盘View的视图Intrinsic Content Size属性。

先设置属性:

然后再调用方法:

大概demo如下:

iOS键盘类型及高度

一、键盘类型

UIKit框架支持8种风格键盘。

typedef enum {

UIKeyboardTypeDefault, // 默认键盘:支持所有字符

UIKeyboardTypeASCIICapable, // 支持ASCII的默认键盘

UIKeyboardTypeNumbersAndPunctuation, // 标准电话键盘,支持+*#等符号

UIKeyboardTypeURL, // URL键盘,有.com按钮;只支持URL字符

UIKeyboardTypeNumberPad, //数字键盘

UIKeyboardTypePhonePad, // 电话键盘

UIKeyboardTypeNamePhonePad, // 电话键盘,也支持输入人名字

UIKeyboardTypeEmailAddress, // 用于输入电子邮件地址的键盘

} UIKeyboardType;

用法用例:

textView.keyboardtype= UIKeyboardTypeNumberPad;

UIKeyboardTypeDefault:常用于文本输入

UIKeyboardTypeASCIICapable:常用于密码输入

UIKeyboardTypeNumbersAndPunctuation:和上一个键盘互相切换

UIKeyboardTypeURL:适用于网址输入

UIKeyboardTypeNumberPad:只有数字的数字键盘

UIKeyboardTypePhonePad:可用于拨号的数字键盘,带*#+

UIKeyboardTypeNamePhonePad:字母及数字键盘

UIKeyboardTypeEmailAddress:适用于邮件地址输入的键盘

UIKeyboardTypeDecimalPad:带“点”的数字键盘,可用于带有小数点的数字输入

UIKeyboardTypeTwitter

UIKeyboardTypeWebSearch:适用于网页搜索的键盘

UIKeyboardTypeAlphabet

二 键盘高度

以下几种键盘类型几乎一样,键盘高度也是一样的

UIKeyboardTypeAlphabet

UIKeyboardTypeASCIICapable

UIKeyboardTypeDefault

UIKeyboardTypeEmailAddress

UIKeyboardTypeNamePhonePad

UIKeyboardTypeNumbersAndPunctuation(数字和标点符号)

UIKeyboardTypeTwitter

UIKeyboardTypeURL

UIKeyboardTypeWebSearch

5.5吋271

4.7吋258

4.0吋253

②以下几种键盘为数字类型的键盘,键盘高度也是一样的

UIKeyboardTypeDecimalPad(带小数点的数字键盘)

UIKeyboardTypeNumberPad(纯数字键盘)

UIKeyboardTypePhonePad(带*+#,;的数字键盘)

5.5吋226

4.7吋216

4.0吋216

链接:

链接:

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

在ios开发中,键盘很常用。在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];}

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

-(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输入法如何调整键盘高度
浏览地址:http://cdkjz.cn/article/phhdjp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220