自己动手写一个自定义弹出框(二)-MGPopController

前几天,我写了一篇介绍如何自己动手写一个自定义弹出框的文章(传送门:“自己动手写一个自定义弹出框-MGPopController”),这篇算是续作吧,主要介绍下如何给弹出框添加UITextField输入框,目前源码和demo我已经更新到github上(任意门:MGPopController)

实现方式

我们都知道UIAlertController里是可以添加UITextField的,所以我按照UIAlertController的方式也设计了一个api:

- (void)addTextFieldWithConfiguration:(void (^)(UITextField *textField))configuration;

具体实现:

- (void)addTextFieldWithConfiguration:(void (^)(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);
    }
}

添加了UITextField,具体怎么展示就是UI布局的问题了,我说过,UI不是重点,你只要根据自己的需要做处理就好了。

使用方法

在Demo里我用一个登陆界面展示了具体的用法,先看一下实现后的效果:


screenshot4.gif

具体代码:

            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;

几点说明

  • 对UITextField可以指定delegate,这样就可以使用UITextField的这些代理方法了,比如限制输入框最大输入字符:
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSInteger length = textField.text.length - range.length + string.length;
    if (length > 11) {
        return NO;
    }
    return YES;
}
  • 获取UITextField中值的方法:
    在MGPopController中已经暴露了只读的textFields属性,可以通过这个属性获取所有添加的UITextField,然后就能获得每个textField里的值了
@property (nonatomic, readonly) NSArray *textFields;
  • 如何做到点击操作按钮的时候不隐藏弹出框,而是在适当的时候消失,比如登录的时候,希望能在从服务端请求回来的时候再关闭输入框
    1.利用MGPopAction里的autoDimiss属性,设置为NO,就不自动消失
MGPopAction *action = [MGPopAction actionWithTitle:@"登录" action:^{
}];
action.autoDismiss = NO;

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

    //模拟http请求异步返回数据
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"登录成功...");
        [self.loginController dismiss];
    });
  • 如何做到弹出框隐藏时,先隐藏键盘,再隐藏弹出框
    知道这个问题的我想肯定被UIAlertController折磨过,因为在UIAlertController里,是没法做到先隐藏键盘,再隐藏提示框的,至少我没有找到解决办法(如果有人知道请你一定要告诉我_
    处理方法是在操作按钮的action里去隐藏键盘,因为我在MGPopController里是先调用的action这个block,再隐藏的弹出框,demo里我是利用IQKeyboardManager处理的(其实把这个功能封装到MGPopController里也是可以的):
[pop addAction:[MGPopAction actionWithTitle:@"关闭" action:^{
       NSLog(@"关闭...");
       [[IQKeyboardManager sharedManager] resignFirstResponder];
}]];
  • 关于键盘的处理
    输入框会被弹起的键盘遮挡住,这个问题很常见,相信大家肯定遇到过。然而我在MGPopController里并没有增加对键盘的处理,因为实际项目中我一般都会使用IQKeyboardManager这个第三方库来处理键盘,你几乎不用写一行代码就可以完美解决这个问题。我在demo里也引入了这个库,测试过后效果,虽然不是很完美,但是也还可以。如果你接受不了,你可以自己添加对键盘的处理,思路大概如下:
    1.在MGPopController里注册键盘通知
    2.根据需要,动态修改containerView(弹出框的所有View都添加到这个View里的)垂直方向偏移量,可以适当添加点动画,不至于显得很生硬。
    3.别忘了,在dealloc里移除键盘通知

最后

UI不是重点,重点是思路,希望对你能有一丢丢帮助。Enjoy yourself!
(文笔、技术能力有限,如果发现哪里写错了或者说错了,非常欢迎您能不吝赐教,感激不尽!)

你可能感兴趣的:(自己动手写一个自定义弹出框(二)-MGPopController)