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:^{
    }];
来张效果图:

UIAlertController更改内容样式及插入自定义View_第1张图片

1

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

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

- (void)touchesBegan:(NSSet *)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:^{
    }];
}

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


你可能感兴趣的:(Objective-C)