控件类首页投稿(暂停使用,暂停投稿)iOS学习开发

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

2016-08-08  本文已影响630人  是的蛮大人

前几天,我写了一篇介绍如何自己动手写一个自定义弹出框的文章(传送门:“自己动手写一个自定义弹出框-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;

几点说明

 - (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;
}
@property (nonatomic, readonly) NSArray<UITextField *> *textFields;
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];
    });
[pop addAction:[MGPopAction actionWithTitle:@"关闭" action:^{
       NSLog(@"关闭...");
       [[IQKeyboardManager sharedManager] resignFirstResponder];
}]];

最后

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

上一篇 下一篇

猜你喜欢

热点阅读