博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
完美解决ios4与ios5输入框随键盘移动问题
阅读量:5143 次
发布时间:2019-06-13

本文共 3119 字,大约阅读时间需要 10 分钟。

iOS5中当键盘输入法切换到中文时,键盘高度由216增加到252像素。这一变化将遮住输入框。如何才能解决这一问题呢?

      在iOS5中,新增有notification(UIKeyboardWillChangeFrameNotification)可以用来监测键盘frame的变化。在iOS4中,可以通过UIKeyboardWillShowNotification以及UIKeyboardWillHideNotification来监测键盘的显示与隐藏事件。综合处理下,可以用以下方法解决:

#ifndef IOS_VERSION  #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]  #endif

在viewdidload中注册监测事件:

if (IOS_VERSION < 5.0) {      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillShowNotification object:nil];      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillHideNotification object:nil];  }else{      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];  }

在dealloc中移除监测事件:

if (IOS_VERSION < 5.0) {      [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];      [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];  }else{      [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];  }

事件处理函数:

- (void)keyboardWillChangeFrame:(NSNotification *)notification{      if(!isDisplayFaceBox){  #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2          if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {  #endif  #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2              NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];  #else              NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];  #endif              CGRect keyboardBounds;              [keyboardBoundsValue getValue:&keyboardBounds];              //UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);              //[keyboardScrollView setScrollIndicatorInsets:e];              //[keyboardScrollView setContentInset:e];                            NSInteger offset = 480-keyboardBounds.origin.y;              CGRect listFrame = CGRectMake(0, offset, 320, 377-offset);                            [UIView beginAnimations:@"anim" context:NULL];              [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];              [UIView setAnimationBeginsFromCurrentState:YES];              [UIView setAnimationDuration:0.3];              //处理移动事件,将各视图设置最终要达到的状态              [tableviewbo setFrame:listFrame];                            faceButton.hidden = NO;              keyboardButton.hidden = YES;                                [keyboardScrollView setContentOffset:CGPointMake(0, offset) animated:NO];                            [self scrollToBottomAnimated:NO];                            [UIView commitAnimations];                #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2          }  #endif      }  }

原文地址:http://blog.csdn.net/zengconggen/article/details/7071086

转载于:https://www.cnblogs.com/appwgh/archive/2012/05/25/2517417.html

你可能感兴趣的文章
实验四2
查看>>
Android现学现用第十一天
查看>>
多路复用
查看>>
Python数据可视化之Pygal(雷达图)
查看>>
Java学习笔记--字符串和文件IO
查看>>
转 Silverlight开发历程—(画刷与着色之线性渐变画刷)
查看>>
SQL语法(3)
查看>>
在js在添版本号
查看>>
sublime3
查看>>
Exception Type: IntegrityError 数据完整性错误
查看>>
Nuget:Newtonsoft.Json
查看>>
【luogu4185】 [USACO18JAN]MooTube [并查集]
查看>>
手机号脱敏处理
查看>>
CI控制器调用内部方法并载入相应模板的做法
查看>>
Hdu - 1002 - A + B Problem II
查看>>
HDU - 2609 - How many
查看>>
每天CookBook之Python-003
查看>>
每天CookBook之Python-004
查看>>
Android设置Gmail邮箱
查看>>
StringBuffer的用法
查看>>