【问题标题】:UIPickerView set selected row background color (now also for iOS 14)UIPickerView 设置选定行背景颜色(现在也适用于 iOS 14)
【发布时间】:2014-12-17 02:32:02
【问题描述】:

我找到了这个代码here:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (UILabel*) view;
    if (label == nil)
    {
        label = [[UILabel alloc] init];
    }

    [label setText:@"Whatever"];

    // This part just colorizes everything, since you asked about that.

    [label setTextColor:[UIColor whiteColor]];
    [label setBackgroundColor:[UIColor blackColor]];
    CGSize rowSize = [pickerView rowSizeForComponent:component];
    CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
    [label setFrame:labelRect];

    return label;
}

但是,正如 cmets 中所说,它有一个问题:

它为标签着色,而不是选择器本身。因此,当您滚动到一端或另一端时,会出现一个空白点,您可以在其中看到白色背景。

我只需要更改所选行的背景颜色。

【问题讨论】:

  • 您能否发布意外行为的屏幕截图?

标签: ios objective-c xcode xcode6 uipickerview


【解决方案1】:

我们也可以通过继承 UIPickerView 并将 Andy 的答案放在 layoutSubviews 覆盖的方法中来实现。

class PickerView: UIPickerView {
   override func layoutSubviews() {
      super.layoutSubviews()
      subviews[safe:1]?.backgroundColor = UIColor.clear 
   }
}

【讨论】:

    【解决方案2】:

    我发现在 iOS 14 中,我现在在选择器的选定项目上获得灰色背景颜色。目前的要求是摆脱它。 所以这似乎可行。

    pickerView.subviews[safe:1]?.backgroundColor = UIColor.clear 
    

    ("safe" 是一个进行数组边界检查的运算符,如果索引无效则返回 nil)

    我从这里得到了这个想法:https://stackoverflow.com/a/53740571/826946

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多