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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27174050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:16:12  来源:igfitidea点击:

Change colour of UIPickerView selection indicators

iosiphonexcodeipaduipickerview

提问by cjhill

Our UIPickerView has a dark background and the selection lines are incredibly hard to see. Is there anyway to change them to a colour of my choosing?

我们的 UIPickerView 有一个黑暗的背景,选择线非常难以看到。反正有没有把它们改成我选择的颜色?

Update: I'm talking about the --- above and below the "Panda" item.

更新:我说的是“Pandas”项目上方和下方的 ---。

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

采纳答案by App Dev Guy

The best solution I can offer is to place 2 UIImages on top of your picker wheel at the width of 1 and length set to your preference. This is how I overcame this.

我能提供的最佳解决方案是将 2 个 UIImages 放置在您的选择器轮顶部,宽度为 1,长度设置为您的喜好。我就是这样克服的。

It does not work well if you require multiple size views so programatically doing it to cater for your size changes would be the next best solution. To programatically change the size using if statements:

如果您需要多个尺寸视图,则它不会很好地工作,因此以编程方式执行它以满足您的尺寸变化将是下一个最佳解决方案。要使用 if 语句以编程方式更改大小:

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

如何在iphone的objective-c中以编程方式调整图像大小

If you change the size based on orientation add an ifstatement that handles orientation like so:

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

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

Kind regards

亲切的问候

回答by Dian

Swift 4.2

斯威夫特 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
    }

This works for me!

这对我有用!

回答by ObjectiveTC

This has been answered here: How to change the color of UIPickerView Selector

这已在此处回答:如何更改 UIPickerView 选择器的颜色

Basically:

基本上:

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

回答by Rodrigo Carrilho

Try something like this:

尝试这样的事情:

- (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];
 } 
}

回答by budidino

Add this to your UIPickerView's viewForRow, titleForRowor attributedTitleForRow:

将此添加到您UIPickerViewviewForRow,titleForRowattributedTitleForRow

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

This should be a little bit safer to use because we are looking for subviews that should be very short in height. If Apple changes the view hierarchy it most probably won't mess up anything :)

这应该使用起来更安全一些,因为我们正在寻找高度应该非常短的子视图。如果 Apple 更改视图层次结构,它很可能不会搞砸任何事情:)

回答by zgorawski

This is how I did it in Swift 4.2

这就是我在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

this code can of course be encapsulated in extension if needed.

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