更改UIPickerView选择指示器的颜色

时间:2014-11-27 15:25:11

标签: ios iphone xcode ipad uipickerview

我们的UIPickerView背景较暗,选择线难以辨认。反正有没有把它们变成我选择的颜色?

更新:我在谈论" Panda"的上方和下方。项目

    Cat
    Dog
  Elephant
-----------
   Panda
-----------
   Bear
  Giraffe

7 个答案:

答案 0 :(得分:2)

我能提供的最佳解决方案是将2个UIImages放在拾取轮上,宽度为1,长度根据您的喜好设置。这就是我克服这一点的方式。

如果您需要多个大小的视图,它不能正常工作,因此以编程方式执行此操作以满足您的大小更改将是下一个最佳解决方案。要使用if语句以编程方式更改大小:

How to resize the image programmatically in objective-c in iphone

如果您根据方向更改大小,请添加一个处理方向的if语句,如下所示:

 if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
     // code for landscape orientation      
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
     // code for Portrait orientation       
}

亲切的问候

答案 1 :(得分:2)

这已在此处回答:How to change the color of UIPickerView Selector

基本上:

pickerView.subviews[1].backgroundColor = UIColor.whiteColor()
pickerView.subviews[2].backgroundColor = UIColor.whiteColor()

答案 2 :(得分: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];
 } 
}

答案 3 :(得分:1)

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
    }

这对我有用!

答案 4 :(得分:1)

您可以使用UIPickerView的专用标题来更改选择器视图选择线的颜色。只需为_magnifierLineColor键设置颜色

pickerView.setValue(UIColor.red, forKey: "_magnifierLineColor")

答案 5 :(得分: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

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

答案 6 :(得分:0)

将此添加到您的UIPickerView的{​​{1}},viewForRowtitleForRow

attributedTitleForRow

这应该更安全一些,因为我们正在寻找高度应该很短的子视图。如果Apple更改了视图层次结构,则很可能不会弄乱任何东西:)

相关问题