
5、统一页面弹框样式,更加美观(属性弹框、错误信息提示等)。在基于mv+easyui的web开发框架里面,大量采用了jquery的方法,对数据进行请求或者提交ios键盘顶部显示短信验证码,方便页面和服务器后端进行数据的交互处理。虽然多余的空格并不能影响html的页面展示样式,但是服务端页面渲染和网络数据传输这些空格和其他字符没有区别,同样要做处理,这样的话,这些空格就会造成时间和空间维度上的浪费,所以完全可以将多个连续的空格合并成一个,从而既减少了字符又不会影响页面展示。

可以看到,当我们点击位于屏幕下方的TextField时,弹出键盘,但随之TableView也往上滑动了一下,这样就避免了键盘遮挡输入框的问题。根据上一篇文章的知识点,实现的方式相信大家应该很容易就能想到,无非是到键盘弹出时更改TableView的ContentOffset值。这里,笔者做了个简单的Demo,实现的效果如下:

代码应该也很好理解:
-(void)viewDidLoad{
[super viewDidLoad];
//键盘弹出通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShowWithNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
//在Cell中加入UITextField,并设置Delegate为Self
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, 24)];
field.delegate = self;
field.placeholder = [NSString stringWithFormat:@"You can input Something At Index %li",(long)indexPath.row];
[cell.contentView addSubview:field];
}
return cell;
}
//在即将编辑之前计算出TextField的位置,并转化成TableView中的位置
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.activedTextFieldRect = [textField convertRect:textField.frame toView:self.tableview];
}

//当键盘弹出时,动态改变TableView的ContentOffset的值
- (void)keyBoardWillShowWithNotification:(NSNotification *)notification {
//get FrameEnd
CGRect rect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//get AnimationDuration
double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//keyboard height > textView height, need scroll
if ((self.activedTextFieldRect.origin.y + self.activedTextFieldRect.size.height) > ([UIScreen mainScreen].bounds.size.height - rect.size.height))
{
[UIView animateWithDuration:duration animations:^{
self.tableview.contentOffset = CGPointMake(0, 64 + self.activedTextFieldRect.origin.y + self.activedTextFieldRect.size.height - ([UIScreen mainScreen].bounds.size.height - rect.size.height));
}];
}
}
以上代码可以点击这里从github获取到
可以看到,这么处理的一个繁琐的地方是,必须要在TableViewCell中获取到TextField,并将TextField的Delegate设置为当前的控制器。这显然不是一个好的解决方案,那有没有更好的解决方案呢,答案是肯定的,只是大家没想到会有多么简单:
在工程的Podfile中添加如下这句话:
pod 'IQKeyboardManager'
即可。是的,引入IQKeyboardManager库即可。他会自动帮我们解决如上问题,下面我们看一下删除我们刚刚的代码,并加入库IQKeyboardManager后的效果:

可以看到我们的键盘上面多了一个小的工具栏,可以选择上下箭头来切换需要填写的TextField,我们的PlaceHolder也显示在了这个小工具栏上。鉴于有些读者可能对这个库的使用不是很熟悉,这里对其使用做个简单的介绍:

IQKeyboardManager的开启/关闭
[IQKeyboardManager sharedManager].enable = YES;
lan optionrom:开启/关闭网卡启动rom,设置项有disabled(关闭)/enabled(开启),默认是disabled(关闭),如果需要从板载网卡启动,要设置为enabled(开启)。夜间模式开启后,可以设置led关闭时间段ios键盘顶部显示短信验证码,默认夜间模式为关闭,需要自行开启,小智助手默认提供两个时间段(23:00、07:00),点击默认时间可以设定时间。 股票行情即时看功能在您安装ie伴侣后是默认开启,快捷键 f8 控制“开启/关闭” 显示。
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[IQKeyboardManager sharedManager].enable = NO;
}
-(void)viewWillDisappear:(BOOL)animated {
[IQKeyboardManager sharedManager].enable = YES;
[super viewWillDisappear:animated];
}
或者在 AppDelegate中注册方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[IQKeyboardManager sharedManager] disableInViewControllerClass:[XXXViewController class]];
}
IQKeyboardManager工具条的显示/隐藏
[IQKeyboardManager sharedManager].enableAutoToolbar = NO;

设置点击背景收回键盘
[IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
下面我们来研究一下这个库的源代码。有过一定开发经验的iOS程序员肯定是知道要从load方法开始找起,因为这个库没被引用即可生效,那99%的可能性是使用了这个iOS中的“黑魔法”。
load方法位于类IQKeyboardManager中,实现如下:
+(void)load
{
//Enabling IQKeyboardManager. Loading asynchronous on main thread
[self performSelectorOnMainThread:@selector(sharedManager) withObject:nil waitUntilDone:NO];
}
这里调用remove方法后并没有像before跟after中将通知方法彻底移除,注册过的环绕方法仍然会存在内存中,所以这个方法无法移除环绕通知,仅仅是避免了在函数链中执行它而已。*:application_start() 此函数是我们程序刚启动的时候调用的函数,相当于我们c语言时候的main函数一样,都是程序一开始执行的函数,可以将一些需要初始化的函数,方法,写在这里,比如路由,日志,ioc,di,区域,文件等,关闭的时候有个对应的方法application_end()函数。cwnd类的成员初始化对话框项函数初始化消息处理函数窗口状态函数数据绑定函数系统消息处理函数窗口大小和位置菜单函数一般消息处理函数窗口访问函数工具提示函数控件消息处理函数更新/绘图函数定时器函数输入消息处理函数坐标映射函数警告函数非客户区消息处理函数窗口文本函数窗口消息函数mdi消息处理函数滚动函数剪贴板函数剪贴板消息处理函数拖放函数ole控件菜单循环通知插字符号函数可重载函数。
[strongSelf registerAllNotifications];
这个方法的实现中,最主要的是了UITextFieldTextDidBeginEditingNotification通知

[self registerTextFieldViewClass:[UITextField class] didBeginEditingNotificationName:UITextFieldTextDidBeginEditingNotification didEndEditingNotificationName:UITextFieldTextDidEndEditingNotification];
因此,当UITextField获取焦点后,在方法
-(void)textFieldViewDidBeginEditing:(NSNotification*)notification
中进行了添加键盘工具栏,调整ScrollView等可能被键盘遮挡的View的相应属性。具体的调整方法位于
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/sanxing/article-115497-1.html
穿白毛衣太棒
不明白的是号称民主国家的美国竟不顾天下苍生
您好