【问题标题】:UISlider setMaximumTrackTintColorUISlider setMaximumTrackTintColor
【发布时间】:2014-03-08 03:31:23
【问题描述】:

我正在尝试在 UISlider 上动态配置轨道颜色。

这行代码非常适合设置滑块轨道的低端。

[self.sliderBar setMinimumTrackTintColor:[UIColor redColor]];

当尝试对滑块轨道的高端执行相同操作时,这行代码会向调试日志报告 3 个致命错误。

[self.sliderBar setMaximumTrackTintColor:[UIColor redColor]];

<Error>: CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

<Error>: clip: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

<Error>: CGContextDrawLinearGradient: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context  and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

我也尝试使用点表示法设置轨道色调颜色,但报告了相同的 3 个错误。

self.sliderBar.maximumTrackTintColor = [UIColor redColor];

我很困惑为什么正确设置了最小轨道色调颜色,而最大轨道色调颜色却被打破了。任何帮助将不胜感激。

这个问题似乎与this 问题有关,但我不能 100% 确定,因为我不使用图形(仅设置色调)。

【问题讨论】:

    标签: ios objective-c ios7 uislider


    【解决方案1】:

    这很奇怪,最小和最大对我来说都很好,我什至可以让它动画颜色变化。我只能建议重新创建滑块和@synthesize。

    @synthesize slider;
    
    - (void)viewDidLoad
    {
    [super viewDidLoad];
    [slider setMinimumTrackTintColor:[UIColor orangeColor]];
    [slider setMaximumTrackTintColor:[UIColor blueColor]];
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
    }
    

    【讨论】:

    • @VincentDucastel 点表示法实际上在幕后调用了相同的二传手。除了你传入的颜色之外,这两个 sn-ps 是等价的。
    • 抱歉,我的测试过程中一定是错的……但我无法通过 Interface Builder 更改 maximumTrackTintColor……我编辑了我的答案。谢谢。
    【解决方案2】:

    我认为这是 Apple 的问题。有很多人在 7.1 中报告了奇怪的 UISlider 行为。我个人无法改变最小的色调。我认为你目前无能为力。

    【讨论】:

      【解决方案3】:

      这是一个 Apple 错误,它仍然存在于 iOS 8 中。我在尝试通过子类上的代理设置器修改滑块的最大色调颜色时看到了它。更改最小色调颜色可以正常工作并且没有显示错误,但是尝试更改最大色调颜色会引发大量错误的上下文错误,并且经常将滑块渐变的那部分绘制在错误的位置(这证明它确实没有良好的上下文)。

      我找不到该问题的解决方法,但会提交错误报告。

      【讨论】:

      • 我采用的解决方法是创建一个具有所有相同属性的全新 UISlider,丢弃原来的 UISlider,然后将新的 UISlider 放在原来的位置。
      【解决方案4】:

      避免冗余设置 maximumTrackTintColor。当我的代码两次更新颜色时,最大线就会消失。我可以用一个简单的 UISlider 重现它,如下所示:

          UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 150, 23)];
          slider.maximumTrackTintColor = [UIColor whiteColor];
          slider.maximumTrackTintColor = [UIColor whiteColor];
      

      在 iOS 8 下,此滑块在屏幕上显示时将没有最大轨道,而不是白色轨道。

      【讨论】:

      • 当您在情节提要和代码中同时设置 maximumTrackTintColor 时,也会重现此问题。
      • 我不知道其他 iOS 版本,但在 8.4.1 中,检查 maximumTrackTintColor 的值并将其分配给所需的颜色,前提是该颜色还没有工作。我必须将 maximumTrackTintColor 设置为我不想要的颜色,然后将其设置为我想要的颜色。使用不等式“if”语句,最大值线消失。
      【解决方案5】:

      IOS 8.3
      它与这些方法工作

      [slider setMinimumTrackTintColor:[UIColor orangeColor]];
      [slider setMaximumTrackTintColor:[UIColor blueColor]];
      

      或:

      slider.minimumTrackTintColor = [UIColor orangeColor];
      slider.maximumTrackTintColor = [UIColor blueColor];
      

      编辑
      抱歉,我在测试过程中弄错了……但我无法通过 Interface Builder 更改 ma​​ximumTrackTintColor……
      如果要更改 UISlider 的 ma​​ximumTrackTintColor

      ,请使用上述方法

      【讨论】:

      • 点符号实际上在幕后调用了同一个 setter。这两个 sn-ps 是等价的,除了你传入的颜色。
      • 抱歉,我在测试过程中必须出错……但我无法通过 Interface Builder 更改 maximumTrackTintColor……我编辑了我的答案。谢谢。
      【解决方案6】:

      对我来说,问题的发生是因为我将UISlider 子类化为创建自定义trackRectForBounds,该trackRectForBounds 返回一个大小无效的CGRect。我的原始实现(可能还有一些 iOS 版本的实现?)根据 UISlider 的边界返回一个大小为零或负数的轨道边界。看来这与设置minimum/maximumTrackTintColor会调用trackRectForBounds有关,可能是为了创建轨道图像。

      解决此问题的方法是确保在设置最小/最大轨道色调颜色时轨道边界有效。

      选项 1:使用大小足以容纳 UISlider 的 initWithFrame

      UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0,0,100,38)];
      slider.minimumTrackTintColor = [UIColor greenColor];
      slider.maximumTrackTintColor = [UIColor orangeColor];
      // ... rest of view setup ...
      

      选项 2:添加滑块并强制自动布局,以便在设置色调颜色之前正确调整大小

      UISlider *slider = [[UISlider alloc] init]; // <-- problem, zero size
      [view addSubview:slider];
      // ... other view setup, including constraints ...
      [view layoutIfNeeded]; // forced layout to give slider a non-zero size
      slider.minimumTrackTintColor = [UIColor greenColor];
      slider.maximumTrackTintColor = [UIColor orangeColor];
      

      选项 3:修复覆盖以确保无论边界如何,它都会返回一个非零大小的矩形。

      - (CGRect)trackRectForBounds:(CGRect)bounds {
           // example, inset left/right by 2 (thus width - 4)
           // track height is 5 pixels, centered in bounds.
           return CGRectMake(
               CGRectGetMinX(bounds) + 2,
               CGRectGetMinY(bounds) + (CGRectGetHeight(bounds) + 5)/2.0,
               MAX(10, CGRectGetWidth(bounds) - 4), // use max make sure positive.
               5)
      }
      

      注意:我在 viewDidLoad 之后创建 UISliders 所以 Ahufford's answer 对我不起作用,但我想这也可以解释它。我也猜想Ahufford's answer 在大多数情况下可能效果最好。

      【讨论】:

      • 我对子类 UISlider 有类似的问题。它有一个在 initWith* 方法中加载的自定义背景。但是,在超级初始化期间调用了 trackRectForBounds,它引用了尚未加载的背景。你为我节省了大量的时间来追踪这个!谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多