但是不能一直给他添加手势,具体怎么展示就是

2019-09-13 11:20栏目:大奖888官网登录
TAG:

  • 思路:
    • 键盘弹出的时候会发出通报
    • 想点击空白遮掩键盘,那就给空白处增添手势
    • 之所以在发生键盘弹出的通报后,登时给keyWindow加多手势监听
    • 假使客户在键盘弹出后,点击空白处,就能暗藏键盘
    • 当收到键盘遮掩的通告,立即移除手势
  • 赢得UIText菲尔德中值的不二秘诀:在MGPopController中一度揭露了只读的textFields属性,能够由此这本天性获取具备增多的UIText菲尔德,然后就能够取得每一种textField里的值了

思路有了,直接上代码:

 //模拟http请求异步返回数据 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSLog(@"登录成功..."); [self.loginController dismiss]; });

关键点在于要在全局有效,所以选拔给window增多手势,不过无法平昔给他丰硕手势,会覆盖掉别的的荧屏触摸事件,所以在键盘隐蔽之后,立即移除掉手势。

@property (nonatomic, readonly) NSArray<UITextField *> *textFields;
//// DismissKeyboard.h// 全局点击空白隐藏键盘//// Created by ljc on 15/7/7.// Copyright  2015年 ljc. All rights reserved.///** 使用方法 * 1.在AppDelegate.m 中 #import "DismissKeyboard.h" * 2.在 - application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions * 加上[self openTouchOutsideDismissKeyboard]; */#import "AppDelegate.h"@interface AppDelegate (DismissKeyboard)/** 开启点击空白处隐藏键盘功能 */- openTouchOutsideDismissKeyboard;@end@implementation AppDelegate (DismissKeyboard)/** 开启点击空白处隐藏键盘功能 */- openTouchOutsideDismissKeyboard{ [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addGesture) name:UIKeyboardDidShowNotification object:nil];}- addGesture{ [self.window addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disappearKeyboard)]];}- disappearKeyboard{ [self.window endEditing:YES]; [self.window removeGestureRecognizer:self.window.gestureRecognizers.lastOb ject];}- dealloc{ [[NSNotificationCenter defaultCenter]removeObserver:self];}@end

在用UITextField的时候,点击空白处掩饰键盘,就像是相当粗略。不过,假设要是成千上万页面都有UITextField,你要把同样的代码,写上很多遍呢?可能导入框架,仅仅达成那样贰个小功效?笔者将付诸那么些主题素材最直白的消除办法!

  • 有关键盘的管理输入框会被弹起的键盘遮挡住,那几个难点很广泛,相信我们自然蒙受过。不过小编在MGPopController里并不曾扩展对键盘的拍卖,因为实际项目中本人一般都会利用IQKeyboardManager那几个第三方库来拍卖键盘,你大致不用写一行代码就足以健全化解那个难题。作者在demo里也引进了这么些库,测验之后效果,固然不是很完美,可是也还是能。要是你接受不了,你能够友善加上对键盘的拍卖,思路大概如下:1.在MGPopController里注册键盘通知2.基于供给,动态修改containerView(弹出框的有所View都助长到这些View里的)垂直方向偏移量,能够适当的量增多点动画,不至于显得很刚毅。3.别忘了,在dealloc里移除键盘文告

运用办法

假定您不想本身敲,直接导入源文件就好:

  1. 在AppDelegate.m 中 #import "DismissKeyboard.h"
  2. - application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 加上[self openTouchOutsideDismissKeyboard];
- addTextFieldWithConfiguration:(UITextField *textField))configuration { UITextField *textField = [[UITextField alloc] init]; textField.borderStyle = UITextBorderStyleRoundedRect; textField.font = [UIFont systemFontOfSize:13.0f]; NSMutableArray *arr = [NSMutableArray arrayWithArray:_textFields]; [arr addObject:textField]; _textFields = arr; if (configuration) { configuration(textField); }}

图片 1screenshot4.gif

咱俩都明白UIAlertController里是足以加多UITextField的,所以自个儿依照UIAlertController的秘技也布署了贰个api:

UI不是重视,珍视是思路,希望对你能有一小点帮忙。Enjoy yourself!(文笔、技手艺量轻便,假如发现哪个地方写错了或许说错了,极其接待您能不吝赐教,感谢不尽!)

- addTextFieldWithConfiguration:(UITextField *textField))configuration;
 - textField:(UITextField *)textField shouldChangeCharactersInRange:range replacementString:(NSString *)string { NSInteger length = textField.text.length - range.length + string.length; if (length > 11) { return NO; } return YES;}
  • 对UITextField可以钦命delegate,那样就足以应用UIText菲尔德的那些代理方法了,举例限制输入框最大输入字符:

具体代码:

2.应用MGPopController里dismiss方法,在需求的时候调用就可以关闭弹出框了

明日,小编写了一篇介绍怎样和谐出手写贰个自定义弹出框的小说(传送门:“自个儿入手写三个自定义弹出框-MGPopController”),那篇算是续作吗,首要介绍下怎么给弹出框增多UITextField输入框,最近源码和demo小编曾经更新到github上(率性门:MGPopController)

  • 怎么成功点击操作按键的时候不掩盖弹出框,而是在适合的时候未有,比方登陆的时候,希望能在从服务端央浼回来的时候再关闭输入框1.施用MGPopAction里的autoDimiss属性,设置为NO,就不自行消失
[pop addAction:[MGPopAction actionWithTitle:@"关闭" action:^{ NSLog; [[IQKeyboardManager sharedManager] resignFirstResponder];}]];

实际落到实处:

 MGPopController *pop = [[MGPopController alloc] initWithTitle:@"登录" message:nil image:nil]; [pop addAction:[MGPopAction actionWithTitle:@"关闭" action:^{ NSLog; [[IQKeyboardManager sharedManager] resignFirstResponder]; }]]; __weak __typeof(&*self)weakSelf = self; MGPopAction *action = [MGPopAction actionWithTitle:@"登录" action:^{ [[IQKeyboardManager sharedManager] resignFirstResponder]; [weakSelf loginWithMobile:[pop.textFields firstObject].text password:[pop.textFields objectAtIndex:1].text]; }]; action.autoDismiss = NO; [pop addAction:action]; [pop addTextFieldWithConfiguration:^(UITextField *textField) { textField.placeholder = @"请输入手机号..."; textField.keyboardType = UIKeyboardTypeNumberPad; textField.delegate = self; }]; [pop addTextFieldWithConfiguration:^(UITextField *textField) { textField.placeholder = @"请输入密码..."; textField.secureTextEntry = YES; }]; pop.titleFont = [UIFont boldSystemFontOfSize:17.0f]; pop.messageFont = [UIFont systemFontOfSize:13.0]; pop.showActionSeparator = YES; pop.actionSpacing = 0; pop.actionPaddingLeftRight = 0; pop.actionPaddingBottom = 0; pop.showCloseButton = NO; [pop show]; self.loginController = pop;

增多了UIText菲尔德,具体怎么显得正是UI布局的主题素材了,小编说过,UI不是注重,你即使依据本身的内需做拍卖就好了。

  • 什么做到弹出框掩盖时,先掩饰键盘,再掩盖弹出框知道那几个难点的本人想一定被UIAlertController折磨过,因为在UIAlertController里,是无语做到先遮掩键盘,再隐敝提示框的,至少自身一贯不找到化解办法(假设有人知晓请您早晚要告诉本人_)管理办法是在操作开关的action里去潜伏键盘,因为自个儿在MGPopController里是先调用的action那个block,再掩盖的弹出框,demo里笔者是行使IQKeyboardManager处理的(其实把那么些意义封装到MGPopController里也是足以的):
MGPopAction *action = [MGPopAction actionWithTitle:@"登录" action:^{}];action.autoDismiss = NO;

在德姆o里本人用二个登入分界面呈现了实际的用法,先看一下跌实后的功力:

版权声明:本文由大奖888-www.88pt88.com-大奖888官网登录发布于大奖888官网登录,转载请注明出处:但是不能一直给他添加手势,具体怎么展示就是