UIAlertController更改内容样式及插入自定义View

iOS8 之后系统把UIActionSheet和UIAlertView合并为了UIAlertController,使用就不比多说了,但在使用的过程中发现样式是固定的,点进去也未发现可以提供更改多样式的属性,所以只能使用KVC在运行时动态访问和修改对象的属性。

但在使用过程中需要知道属性类型对应的key值,这里提供两个方法,是别人写好的,借用一下。

原文链接:帅气的链接

//获得所有变量
- (NSArray *)getAllIvar:(id)object
{
    NSMutableArray *array = [NSMutableArray array];
    
    unsigned int count;
    Ivar *ivars = class_copyIvarList([object class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        const char *keyChar = ivar_getName(ivar);
        NSString *keyStr = [NSString stringWithCString:keyChar encoding:NSUTF8StringEncoding];
        @try {
            id valueStr = [object valueForKey:keyStr];
            NSDictionary *dic = nil;
            if (valueStr) {
                dic = @{keyStr : valueStr};
            } else {
                dic = @{keyStr : @"值为nil"};
            }
            [array addObject:dic];
        }
        @catch (NSException *exception) {}
    }
    return [array copy];
}

//获得所有属性
- (NSArray *)getAllProperty:(id)object
{
    NSMutableArray *array = [NSMutableArray array];
    
    unsigned int count;
    objc_property_t *propertys = class_copyPropertyList([object class], &count);
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertys[i];
        const char *nameChar = property_getName(property);
        NSString *nameStr = [NSString stringWithCString:nameChar encoding:NSUTF8StringEncoding];
        [array addObject:nameStr];
    }
    return [array copy];
}
获取到属性之后就可以去设置样式了
 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n" message:@"请选择时间" preferredStyle:UIAlertControllerStyleActionSheet];
   
    UIAlertAction *fixed = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    //更改标题样式
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"这是一个非常美好的内容"];
    [title addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:22],NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(3, 6)];
    [alert setValue:title forKey:@"attributedTitle"];//attributedMessage改内容样式
    
    //文字颜色
    UIColor *purple = [UIColor purpleColor];
    [cancel setValue:purple forKey:@"titleTextColor"];
    UIColor *green = [UIColor greenColor];
    [fixed setValue:green forKey:@"titleTextColor"];
    
    [alert addAction:fixed];
    [alert addAction:cancel];
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    [rootViewController presentViewController:alert animated:YES completion:^{
    }];
来张效果图:


1

这还不够,我们还可能需要在这里面插入自定义的各种内容,奇葩的是连个代理方法都没有。

这里我们只能把标题或者描述内容牺牲掉,用换行符预留出来空间,然后插入控件。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIDatePicker *datePiker = [[UIDatePicker alloc]init];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
   
    UIAlertAction *fixed = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert.view addSubview:datePiker];
    [alert addAction:fixed];
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    [rootViewController presentViewController:alert animated:YES completion:^{
    }];
}

有好的方法请留言告诉下我!


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值