更改 UIPickerView 选择指示器的颜色

cjh*_*ill 4 iphone xcode uipickerview ipad ios

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

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

    Cat
    Dog
  Elephant
-----------
   Panda
-----------
   Bear
  Giraffe
Run Code Online (Sandbox Code Playgroud)

Dia*_*ian 10

斯威夫特 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
    }
Run Code Online (Sandbox Code Playgroud)

这对我有用!


App*_*Guy 2

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

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

如何在 iPhone 的 Objective-C 中以编程方式调整图像大小

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

 if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
     // code for landscape orientation      
}
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
     // code for Portrait orientation       
}
Run Code Online (Sandbox Code Playgroud)

亲切的问候