Ios 更改UIPickerView选择指示器的颜色
我们的UIPickerView有一个黑暗的背景和选择线是难以置信的难以看到。有没有办法把它们换成我喜欢的颜色 更新:我说的是---在“熊猫”项目的上面和下面Ios 更改UIPickerView选择指示器的颜色,ios,iphone,xcode,ipad,uipickerview,Ios,Iphone,Xcode,Ipad,Uipickerview,我们的UIPickerView有一个黑暗的背景和选择线是难以置信的难以看到。有没有办法把它们换成我喜欢的颜色 更新:我说的是---在“熊猫”项目的上面和下面 试着这样做: - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { //On Selecting the component row if (component
试着这样做:
- (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];
}
}
我能提供的最佳解决方案是将2个UIImages放置在拾取轮的顶部,宽度为1,长度根据您的喜好设置。我就是这样克服的 如果您需要多个尺寸视图,那么它就不能很好地工作,因此通过编程来满足您的尺寸变化将是下一个最好的解决方案。要使用if语句以编程方式更改大小,请执行以下操作: 如果根据方向更改大小,请添加一条处理方向的
If
语句,如下所示:
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
亲切问候这里已经回答了这个问题: 基本上:
pickerView.subviews[1].backgroundColor = UIColor.whiteColor()
pickerView.subviews[2].backgroundColor = UIColor.whiteColor()
这就是我在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
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
}
如果需要,此代码当然可以封装在扩展中。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
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
}
这对我有用 将此添加到您的
UIPickerView
的视图for row
,标题forrow
或属性标题forrow
:
for lineSubview in pickerView.subviews {
if lineSubview.frame.height < 1 {
lineSubview.backgroundColor = UIColor.white.withAlphaComponent(0.15)
}
}
pickerView.subview中lineSubview的{
如果lineSubview.frame.height<1{
lineSubview.backgroundColor=UIColor.white.withAlphaComponent(0.15)
}
}
这应该更安全一些,因为我们正在寻找高度应该非常短的子视图。如果Apple更改视图层次结构,它很可能不会弄乱任何东西:)您可以使用
UIPickerView的私有标题更改选择器视图选择行的颜色。只需为\u放大镜linecolor
键设置颜色
pickerView.setValue(UIColor.red,forKey:“\u放大镜linecolor”)
谢谢,@rodrigo carrilho,不过这只会改变文本的颜色。我真正需要的是所选项目上方和下方的线条。我已经更新了我的问题,为我的问题提供了更多的指导。这个答案在不久前就奏效了。更少的发布代码,希望仍然可以工作:这不是一个理想的解决方案,因为我认为这将是相当微妙的,但它会做的工作。如果你这样做的话,最好将showselectionindicator
设置为NO
。我完全同意“不理想”的说法,因此我在我的一些应用程序中加入了计算未来或过去尺寸变化和方向的需要,我只是希望有一个与你不一样的新解决方案,但遗憾的是,事实并非如此。虽然iOS框架非常强大,但它在许多方面都很有限,这就是其中之一。幸运的是,随着他们向快速和更多的web代码(如样式和编码)发展,灵活性也应该增加。很高兴我能提供帮助。@Deyaelden它仍然有效,不过您必须记住,在创建对象时,选择器子视图层次结构可能尚未完全构建,因此,您可能需要将调用推迟到例如查询数据源的时间。@Deyaelden将代码放入titleForRow
或使用子类,例如-(void)willMoveToSuperview:(UIView)newSuperview{[super willMoveToSuperview:newSuperview];self.backgroundColor=[LTPickerView外观].backgroundColor;}-(void)didAddSubview:(UIView*)子视图{[super didAddSubview:subview];if(subview.bounds.size.height)非常感谢!在我的例子中,我设置了.clear color确实隐藏了分隔线的pickerView.subviews.forEach{$0.backgroundColor=.clear}'在iOS 14中,我使用了它的一个变体来去除选择的灰色高亮背景色:(safe是一个进行范围更改的操作符,如果超出范围则返回nil):pickerView.subviews[safe:1]?.backgroundColor=UIColor.clear