【问题标题】:Change colour of UIPickerView selection indicators更改 UIPickerView 选择指示器的颜色
【发布时间】:2014-11-27 15:25:10
【问题描述】:

我们的 UIPickerView 具有深色背景,并且选择线非常难以看到。有没有办法把它们改成我选择的颜色?

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

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

【问题讨论】:

标签: ios iphone xcode ipad uipickerview


【解决方案1】:

斯威夫特 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 color do hide 这个分隔线 'pickerView.subviews.forEach { $0.backgroundColor = .clear }'
  • 在 iOS 14 中,我使用它的一个变体来消除选择的灰色突出显示背景颜色:(安全是一个操作符,它会改变范围并在超出范围时返回 nil):pickerView. subviews[safe:1]?.backgroundColor = UIColor.clear
【解决方案2】:

这里已经回答了这个问题:How to change the color of UIPickerView Selector

基本上:

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

【讨论】:

  • @DeyaEldeen 它仍然有效,但您必须记住,在您创建对象时,选择器子视图层次结构可能尚未完全构建,因此您可能需要将调用推迟到例如查询数据源的时间。
  • @DeyaEldeen 将代码放入 titleForRow 或使用子类,例如-(void)willMoveToSuperview:(UIView )newSuperview { [super willMoveToSuperview:newSuperview]; self.backgroundColor = [LTPickerView 外观].backgroundColor; } -(void)didAddSubview:(UIView *)subview { [super didAddSubview:subview]; if ( subview.bounds.size.height 外观TintColor = [LTPickerView外观].tintColor; if ( appearanceTintColor ) { subview.backgroundColor = appearanceTintColor; } } }
【解决方案3】:

我能提供的最佳解决方案是将 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       
}

亲切的问候

【讨论】:

  • 不是一个理想的解决方案,因为我认为它会非常微妙,但它会完成工作。如果您尝试这样做,最好将showsSelectionIndicator 设置为NO
  • 我完全同意“不理想”,因此为什么我需要计算任何未来或过去的尺寸变化和方向,就像我在一些应用程序中必须做的那样,我只是希望有一个更新的解决方案与您自己不同,但遗憾的是没有。虽然 iOS 框架非常强大,但它在许多方面都受到限制,这就是其中之一。幸运的是,随着他们转向 Swift 和更多的 Web 代码,如样式和编码,灵活性也应该增加。很高兴我能帮上忙。
【解决方案4】:

将此添加到您的UIPickerViewviewForRowtitleForRowattributedTitleForRow

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

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

【讨论】:

    【解决方案5】:

    试试这样的:

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

    【讨论】:

    • 谢谢,@rodrigo-carrilho,但这只会改变文本颜色。我真正需要的是所选项目上方和下方的线条。我更新了我的问题,以便为我的问题提供更多指导。
    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-09
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多