我们的UIPickerView有一个黑暗的背景,选择线非常难以看到。有没有办法将它们更改为我选择的颜色?
更新:我指的是上下"熊猫"项目的线。
更新:我指的是上下"熊猫"项目的线。
Cat
Dog
Elephant
-----------
Panda
-----------
Bear
Giraffe
Cat
Dog
Elephant
-----------
Panda
-----------
Bear
Giraffe
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
}
这对我很有效!
pickerView.subviews[1].backgroundColor = UIColor.whiteColor()
pickerView.subviews[2].backgroundColor = UIColor.whiteColor()
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 if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
顺祝商祺
showsSelectionIndicator
设置为 NO
。 - cjhillUIPickerView
的viewForRow
、titleForRow
或attributedTitleForRow
中: 最初的回答:
for lineSubview in pickerView.subviews {
if lineSubview.frame.height < 1 {
lineSubview.backgroundColor = UIColor.white.withAlphaComponent(0.15)
}
}
- (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];
}
}
UIPickerView
的私有头文件更改选择行颜色。只需为_magnifierLineColor
键设置颜色即可。pickerView.setValue(UIColor.red, forKey: "_magnifierLineColor")
这是我在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
如果需要,这段代码当然可以封装在扩展中。