美文网首页
iOS 自定义弹窗UIAlertController

iOS 自定义弹窗UIAlertController

作者: pokeey | 来源:发表于2019-11-27 13:22 被阅读0次
    alert.jpg

    ZFAlertController 是一款使用方便高度自定义的iOS弹窗控件

    Github: https://github.com/ICU-Coders/ZFAlertController

    Usage

    使用方法完全和 UIAlertController 相同

    创建一个普通弹窗

    alert
    ZFAlertController *alertVC = [ZFAlertController alertWithTitle:@"ZFAlertController" message:@"alertWithTitle:message:style:" style:ZFAlertControllerStyleAlert];
    
    ZFAlertAction *ok = [ZFAlertAction actionWithTitle:@"ok" action:^{
    }];
    ZFAlertAction *cancel = [ZFAlertAction actionWithTitle:@"cancel" action:^{
    }];
    
    [alertVC addAction:ok];
    [alertVC addAction:cancel];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    

    创建一个带有TextFiled的弹窗(自动适应键盘)

    textFiled
    ZFAlertController *alertVC = [ZFAlertController alertWithTitle:@"Alert" message:@"alertWithTitle:message:style:" style:ZFAlertControllerStyleAlert];
    
    [alertVC addTextFiledWithText:@"" placeholder:@"Input..." textFieldTextChangedCallback:^(NSString * _Nonnull text, UITextField * _Nonnull textField) {
        NSLog(@"text1:%@", text);
    }];
    ZFAlertAction *ok = [ZFAlertAction actionWithTitle:@"Ok" action:^{
        NSLog(@"ok");
        [self testFunc];
    }];
    
    [alertVC addAction:ok];
    [self presentViewController:alertVC animated:YES completion:nil];
    

    Action Sheet

    actionSheet
    ZFAlertController *alertVC = [ZFAlertController alertWithTitle:@"ActionSheet" message:@"alertWithTitle:message:style:" style:ZFAlertControllerStyleActionSheet];
    
    ZFAlertAction *ok = [ZFAlertAction actionWithTitle:@"Ok" action:^{
    }];
    ZFAlertAction *cancel = [ZFAlertAction actionWithTitle:@"Cancel" action:^{
    }];
    
    [alertVC addAction:ok];
    [alertVC addAction:cancel];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    

    自定义

    custom

    添加各种自定义View

    [alertVC addCustomView:^UIView * _Nonnull{
        UIView *customView = [[UIView alloc] init];
        [customView setBackgroundColor:[UIColor greenColor]];
        return customView;
    } config:^(UIView * _Nonnull contentView, UIView * _Nonnull customView) {
        [customView setFrame:CGRectMake(contentView.frame.origin.x + 40, contentView.frame.origin.y - 40, contentView.frame.size.width - 40 * 2, 30)];
    }];
    

    添加按钮

    [alertVC addCustomButton:^UIButton * _Nonnull{
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        return button;
    } buttonAction:^(UIViewController * _Nonnull alert) {
        [alert dismissViewControllerAnimated:YES completion:nil];
    } config:^(UIView * _Nonnull contentView, UIView * _Nonnull customView) {
        [customView setFrame:CGRectMake(CGRectGetMaxX(contentView.frame) - 44, contentView.frame.origin.y - 44 - 10, 44, 44)];
    }];
    

    如果有任何问题或建议,请告诉我.
    如果觉得不错,给个赞吧🌟
    谢谢

    相关文章

      网友评论

          本文标题:iOS 自定义弹窗UIAlertController

          本文链接:https://www.haomeiwen.com/subject/fnrpwctx.html