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:^{
    }];
}

有好的方法請留言告訴下我!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章