更改UIPickerView选择指示器的颜色。

5
我们的UIPickerView有一个黑暗的背景,选择线非常难以看到。有没有办法将它们更改为我选择的颜色?
更新:我指的是上下"熊猫"项目的线。
    Cat
    Dog
  Elephant
-----------
   Panda
-----------
   Bear
  Giraffe

这个答案之前是可行的。除了发布代码,希望仍然有效: https://dev59.com/3UnSa4cB1Zd3GeqPOXs7 - App Dev Guy
7个回答

11

Swift 4.2

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

        pickerView.subviews[1].backgroundColor = UIColor.white
        pickerView.subviews[2].backgroundColor = UIColor.white

        return view
    }

这对我很有效!


非常感谢!在我的情况下,我设置了.clear颜色来隐藏这些分隔线。 'pickerView.subviews.forEach { $0.backgroundColor = .clear }' - Alex Kolovatov
在iOS 14中,我使用了这个变量的一个变体来消除选择时的灰色高亮背景颜色:(安全运算符是执行范围更改并在范围外返回nil的运算符):pickerView.subviews [safe:1]?.backgroundColor = UIColor.clear - Andy Weinstein

3
这个问题已经在这里得到了解答:如何更改UIPickerView选择器的颜色 基本上:
pickerView.subviews[1].backgroundColor = UIColor.whiteColor()
pickerView.subviews[2].backgroundColor = UIColor.whiteColor()

@DeyaEldeen,它仍然有效,但是您必须记住,在创建对象时,选择器子视图层次结构可能尚未完全构建,因此您可能需要推迟调用,例如在查询数据源时。 - DrMickeyLauer
1
@DeyaEldeen 要么将代码放在 titleForRow 中,要么使用子类,例如:-(void)willMoveToSuperview:(UIView *)newSuperview { [super willMoveToSuperview:newSuperview];self.backgroundColor = [LTPickerView appearance].backgroundColor;}-(void)didAddSubview:(UIView *)subview { [super didAddSubview:subview];if ( subview.bounds.size.height <= 1.0 ) { UIColor* appearanceTintColor = [LTPickerView appearance].tintColor; if ( appearanceTintColor ) { subview.backgroundColor = appearanceTintColor; } }} - DrMickeyLauer

2
我能提供的最好的解决方案是在您的选择器轮顶部放置2个UIImages,宽度设置为1,长度根据您的喜好设置。这就是我克服这个问题的方法。
如果您需要多个大小视图,则不适用,因此编程处理大小更改将是下一个最佳解决方案。要使用if语句编程更改大小:
如需根据方向更改大小,请添加处理方向的if语句,例如: 如何在iphone中以编程方式调整图像大小
 if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
     // code for landscape orientation      
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
     // code for Portrait orientation       
}

顺祝商祺


不是我想象中的理想解决方案,因为它可能会非常微妙,但它可以完成工作。如果您尝试这样做,最好将 showsSelectionIndicator 设置为 NO - cjhill
我完全同意“不理想”的观点,这也是为什么我在我的一些应用程序中包括计算任何未来或过去的大小调整和方向的需要,就像你一样,我只是希望有一个更新的解决方案,但可悲的是没有。虽然iOS框架非常强大,但在许多方面受到限制,这就是其中之一。幸运的是,随着他们向Swift和更多类似于Web的样式和编码的转变,灵活性也应该增加。不过很高兴我能帮上忙。 - App Dev Guy

2
将以下内容添加到您的UIPickerViewviewForRowtitleForRowattributedTitleForRow中:

最初的回答:

for lineSubview in pickerView.subviews {
  if lineSubview.frame.height < 1 {
    lineSubview.backgroundColor = UIColor.white.withAlphaComponent(0.15)
  }
}

这应该更安全一些,因为我们正在寻找非常短的子视图。如果Apple更改视图层次结构,它很可能不会捣乱 :),这就是“最初的回答”。

1
尝试像这样做一些事情:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    //On Selecting the component row
    if (component == 0) {

    } else if (component == 1) {

        [quantityPickerDelegate didChangeLabelText:[pickerArray objectAtIndex:row]];// delegate passing the selected value
        [pickerView reloadComponent:component]; //This will cause your viewForComp to hit
    }
}

- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
 {
 //...
 //Your usual code
  pickerLabel.textColor = defaultColor;

 if([self.pickerView selectedRowInComponent:component] == row) //this is the selected one, change its color
 {
        pickerLabel.textColor = [UIColor colorWithRed:0.0745 green:0.357 blue:1.0 alpha:1];
 } 
}

1
谢谢,@rodrigo-carrilho,但那只会改变文本颜色。我真正需要的是所选项目上下的线条。我已经更新了我的问题,以便更好地指导我的问题。 - cjhill

1
您可以使用UIPickerView的私有头文件更改选择行颜色。只需为_magnifierLineColor键设置颜色即可。
pickerView.setValue(UIColor.red, forKey: "_magnifierLineColor")

0

这是我在Swift 4.2中的做法

private let pickerSelectionIndicatorHeight: CGFloat = 62.0

    let pickerSelectionIndicatorTopLine = UIView()
    let pickerSelectionIndicatorBottomLine = UIView()

    pickerSelectionIndicatorTopLine.backgroundColor = UIColor.white
    pickerSelectionIndicatorBottomLine.backgroundColor = UIColor.white

    picker.addSubview(pickerSelectionIndicatorTopLine)
    picker.addSubview(pickerSelectionIndicatorBottomLine)

    pickerSelectionIndicatorTopLine.translatesAutoresizingMaskIntoConstraints = false
    pickerSelectionIndicatorTopLine.leadingAnchor.constraint(equalTo: picker.leadingAnchor, constant: 0).isActive = true
    pickerSelectionIndicatorTopLine.trailingAnchor.constraint(equalTo: picker.trailingAnchor, constant: 0).isActive = true
    pickerSelectionIndicatorTopLine.heightAnchor.constraint(equalToConstant: 1).isActive = true
    pickerSelectionIndicatorTopLine.centerYAnchor.constraint(equalTo: picker.centerYAnchor, constant: -(pickerSelectionIndicatorHeight / 2 + 1.0)).isActive = true

    pickerSelectionIndicatorBottomLine.translatesAutoresizingMaskIntoConstraints = false
    pickerSelectionIndicatorBottomLine.leadingAnchor.constraint(equalTo: picker.leadingAnchor, constant: 0).isActive = true
    pickerSelectionIndicatorBottomLine.trailingAnchor.constraint(equalTo: picker.trailingAnchor, constant: 0).isActive = true
    pickerSelectionIndicatorBottomLine.heightAnchor.constraint(equalToConstant: 1).isActive = true
    pickerSelectionIndicatorBottomLine.centerYAnchor.constraint(equalTo: picker.centerYAnchor, constant: (pickerSelectionIndicatorHeight / 2 + 1.0)).isActive = true

如果需要,这段代码当然可以封装在扩展中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接