【问题标题】:Change BG color of SVProgessHud in Objective C在Objective C中更改SVProgessHud的BG颜色
【发布时间】:2013-06-05 06:03:42
【问题描述】:

我曾多次使用 SVProgressHUD 及其酷, 现在的查询是, 有什么方法可以根据状态更改 SVProgressHUD 的颜色吗? 说,

  1. 如果 SUCCESS 则 BG 颜色(深灰色)必须为 GREEN
  2. 如果是 ERROR,那么 BG 颜色必须是 RED
  3. 其他颜色用于加载。

我从前几天开始尝试这个,但一切都失败了。

我知道可以通过创建我自己的自定义视图并相应地实现它来完成,但是如果有人已经创建了它并且我可以使用它,那不是很好。

如果上述更改可以在 SVProgressHUD 中完成,那么它很好,但如果不能,那么任何人都可以为我提供一个替代的 ProgressHUD

编辑:我已经尝试过使用UIAppearance。 问题是我们已经为成功设置了绿色,为错误设置了红色,

现在当第一次成功时,它是默认颜色,之后整个加载将是绿色所以即使第一次发生成功错误,它也会是绿色,

然后在执行此操作后,指示器将变为红色,

所以主要是它不会立即反映,而是在代码执行后第二次反映

【问题讨论】:

    标签: ios objective-c progress


    【解决方案1】:

    你可以这样自定义SVProgressHUD的BG颜色

    -(void)showWithStatusSuccess
      {
      [[SVProgressHUD appearance] setHudBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0.4]];
      [[SVProgressHUD appearance] setHudForegroundColor:[UIColor yellowColor]];
      [[SVProgressHUD appearance] setHudFont:[UIFont fontWithName:@"MarkerFelt-Wide" size:16]];
      [[SVProgressHUD appearance] setHudStatusShadowColor:[UIColor greenColor]];
      [SVProgressHUD showSuccessWithStatus:@"Great Success!"];
     }
    
    
    -(void)showWithStatusError {
       [[SVProgressHUD appearance] setHudBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:0.4]];
    //[[SVProgressHUD appearance] setHudForegroundColor:[UIColor yellowColor]];
       [[SVProgressHUD appearance] setHudFont:[UIFont fontWithName:@"MarkerFelt-Wide" size:16]];
     //[[SVProgressHUD appearance] setHudStatusShadowColor:[UIColor greenColor]];
       [SVProgressHUD showErrorWithStatus:@"Failed with Error"];
      }
    

    编辑: 在您的情况下,您需要在 showSuccessWithStatus 和 showErrorWithStatus Methods.so 中更改 SVProgressHUD 的 BGColor。所以转到 SVProgressHUD.m 文件更改您的代码,如

    #pragma mark - Show then dismiss methods
    
    + (void)showSuccessWithStatus:(NSString *)string {
    [[SVProgressHUD appearance] setHudBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0.4]];
    //[[SVProgressHUD appearance] setHudForegroundColor:[UIColor yellowColor]];
    [[SVProgressHUD appearance] setHudFont:[UIFont fontWithName:@"MarkerFelt-Wide" size:16]];
    [self showImage:[[self sharedView] hudSuccessImage] status:string];
    }
    
    + (void)showErrorWithStatus:(NSString *)string {
    [[SVProgressHUD appearance] setHudBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:0.4]];
    //[[SVProgressHUD appearance] setHudForegroundColor:[UIColor yellowColor]];
    [[SVProgressHUD appearance] setHudFont:[UIFont fontWithName:@"MarkerFelt-Wide" size:16]];
    //[[SVProgressHUD appearance] setHudStatusShadowColor:[UIColor greenColor]];
    //[SVProgressHUD showWithStatus:@"Error"];
    
    [self showImage:[[self sharedView] hudErrorImage] status:string];
    }
    

    希望它对你有用。

    【讨论】:

    • setHudBackgroundColor 似乎不再是 SVProgressHUD 上的方法了:(
    【解决方案2】:

    只有将默认样式更改为自定义后,才能更改颜色。例如,这会将环变为绿色。

    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:[UIColor greenColor]];
    [SVProgressHUD show];
    

    其他自定义方式有here on github

    【讨论】:

    • 如何在 SVProgressHUD 中设置图像代替微调器?
    • @Ramakrishna 我实际上已经有几个月没有接触微调器了,但是如果你查看 SVProgressHUD 类的 .h 文件,就会发现处理图像的属性和方法。不确定他们是否可以更换微调器。您可能需要为此深入研究 .m 文件。
    【解决方案3】:

    MBProgressHUD https://github.com/jdg/MBProgressHUD 也很棒。

    更新: 试试这个:

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.color = [UIColor redColor];
    

    您可以为 MBProgressHUD 创建类别,因此您不必在每次要使用它时都重新定义上述内容。示例:

    + (MBProgressHUD *)showFlashHUDMessageTo:(UIView *) view message:(NSString *) message duration:(NSInteger) duration animated:(BOOL) animated
    {
        MBProgressHUD *flashMessage = [MBProgressHUD showHUDAddedTo:view animated:animated];
        [flashMessage setAnimationType:MBProgressHUDAnimationFade];
        [flashMessage setMode:MBProgressHUDModeText];
        flashMessage.labelText = message;
        [flashMessage setMinShowTime:duration];
        [MBProgressHUD hideHUDForView:view animated:animated];
        return flashMessage;
    }
    

    用于显示 X 持续时间的闪烁消息。您可以为每个场景自定义背景颜色等。希望您有所了解。

    【讨论】:

    • 背景颜色属性改变了整个视图的背景颜色,除了 HUD :)
    • 我指的是 MBProgressHUD 的颜色属性(不要使用 setBackgroundColor!)。我刚试了一下,效果很好。告诉我你是怎么做到的。您还可以自定义 HUD 中的其他颜色。您需要阅读 MBProgressHUD.h 中的文档
    • 好的@maethor,工作得很好,但唯一的事情是我每次需要使用它时都必须分配它,比如分配用于加载,然后分配用于显示成功/错误,使用睡眠隐藏取消隐藏: (
    • 更新了我的答案,展示了如何自定义和重用它。
    • 需要比 SVProgressHUD 做更多的锻炼,但工作正常。谢谢
    【解决方案4】:

    可以在SVProgressHUD本身中完成

    您需要的是一个标志,它显示您是显示错误还是成功。你想做的是找出 SVProgressHUD.m 文件中的- (UIColor *)hudBackgroundColor 方法

    然后你可以设置颜色

    比如说:

    if (obj.isError == 1) { //For Error Status
        return [UIColor colorWithRed:255 green:0 blue:0 alpha:1.0];
    }
    else if (obj.isError == 0) { //For Success Status
        return [UIColor colorWithRed:0 green:255 blue:0 alpha:1.0];
    }
    else {//The Default one OR whatever you like to Set for Loading indicator BG
         return [UIColor colorWithWhite:0 alpha:0.8];
    }
    

    希望对你有帮助

    【讨论】:

    • 刚刚有一个问题,加载后不久如果我需要显示成功/错误,它会显示默认颜色(可能是因为单例)。因此,为了加载,我选择了不同的课程,为了显示红色/绿色,我使用了 SVProgressHud。但没关系,它终于奏效了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多