简体   繁体   English

UIPickerView Swift上的奇怪自定义背景颜色

[英]Strange Custom Background Color on UIPickerView Swift

I'm getting strange colors when assigning a custom UIColor to the background of UIPickerViews. 在将自定义UIColor分配给UIPickerViews的背景时,我会得到奇怪的颜色。 I have created a color for textViews and pickerViews as follows: 我为textViews和pickerViews创建了一个颜色,如下所示:

let myTextViewBackgroundColor = UIColor(red: 192.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 0.35)

And I assign it as: 我把它指定为:

myTextView.backgroundColor = myTextViewBackgroundColor
keywordPicker.backgroundColor = myTextViewBackgroundColor

Here's what it looks like: 这是它的样子: 在此输入图像描述 As you can see, the color of the textViews and the color of the picker are different. 如您所见,textViews的颜色和选择器的颜色是不同的。 The difference seems more pronounced on a device than in this snapshot, but the custom color looks muddy, almost like it is mixed with a default gray background. 设备上的差异似乎比此快照更明显,但自定义颜色看起来很混乱,几乎就像它与默认的灰色背景混合在一起。

If I comment out the pickerView color item above, I get a gray color background: 如果我注释掉上面的pickerView颜色项,我会得到一个灰色背景: 在此输入图像描述 If I choose a standard UIColor, it appears to work correctly. 如果我选择标准UIColor,它似乎正常工作。 UIColor.yellowColor in this instance. 在这个例子中是UIColor.yellowColor。 If I choose white, that looks good, too. 如果我选择白色,那看起来也不错。 在此输入图像描述 I create the pickerView in code and assign it to a textField which has no background color assigned. 我在代码中创建pickerView并将其分配给没有指定背景颜色的textField。

var keywordPicker : UIPickerView?

keywordPicker = UIPickerView()
keywordPicker?.delegate = self
keywordPicker?.dataSource = self

keywordsForItemTextField.inputView = keywordPicker
keywordPicker.backgroundColor = myTextViewBackgroundColor

And for what it's worth, I have a date picker that I created as a modal screen with it's own view controller. 为了它的价值,我有一个日期选择器,我用它自己的视图控制器创建一个模态屏幕。 In that case, the picker is created on the storyboard. 在这种情况下,在故事板上创建选择器。 I still assign my background color with code and that picker shows the correct color. 我仍然使用代码指定我的背景颜色,并且选择器显示正确的颜色。

tkanzakic's answer finally allowed me to change the background color of UIDatePicker. tkanzakic的回答最终允许我改变UIDatePicker的背景颜色。

Here's the Swift 4 version: 这是Swift 4版本:

class CustomDatePicker: UIDatePicker {

    var customBackgroundColor = UIColor.black

    override func willMove(toWindow newWindow: UIWindow?) {
        super.willMove(toWindow: newWindow)

        if newWindow != nil {
            inputView?.backgroundColor = customBackgroundColor
        }
    }
}

Change the customBackgroundColor property to your liking. 根据您的喜好更改customBackgroundColor属性。

我很确定这是UIPickerView总是在它上面有银灰色滤镜的结果(当你把它设置为白色时注意它是灰色的吗?)

This response does not expose the cause of the issue, but I've included my resolution in case it will help others. 这个回复没有揭露问题的原因,但我已经包括了我的决议,以防它将帮助其他人。 I created a label for the selected row display and set the background color of the label. 我为选定的行显示创建了一个标签,并设置了标签的背景颜色。 This label color is true to the custom settings. 此标签颜色适用于自定义设置。

I give a nod to Making App Pie and article https://makeapppie.com/2014/10/21/swift-swift-formatting-a-uipickerview/ which was the inspiration. 我对Make App Pie和文章https://makeapppie.com/2014/10/21/swift-swift-formatting-a-uipickerview/表示赞同,这是灵感。 Set the background to white: 将背景设置为白色:

    keywordPicker?.backgroundColor = UIColor.whiteColor()

Also I added a border so the picker looks like the textViews I created: 我还添加了一个边框,所以选择器看起来像我创建的textViews:

    keywordPicker?.layer.borderColor = UIColor(colorLiteralRed: 212.0/255.0, green: 212.0/255.0, blue: 212.0/255.0, alpha: 1.0).CGColor
    keywordPicker?.layer.borderWidth = 1.0
    keywordPicker?.layer.cornerRadius = 7.0
    keywordPicker?.layer.masksToBounds = true

Setup the label: 设置标签:

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {

    let pickerLabel = UILabel()
    var titleData : String = String()

    if pickerView == keywordPicker {
        titleData = crsuKeywordPickerList![row]
    }
    pickerLabel.text = titleData

    pickerLabel.backgroundColor = myPickerBackgroundColor
    pickerLabel.textAlignment = .Center

    return pickerLabel

}//pickerView:viewForRow:...

The result:

在此输入图像描述

As a second option as discussed above, if you instantiate the picker on the storyboard this problem does not appear.

In our case we have a wrapper around the UIPickerView to build a custom date picker. 在我们的例子中,我们有一个围绕UIPickerView的包装器来构建一个自定义日期选择器。 After setting the appearance proxy of the UITextField class to use dark mode for the inputView we have the same glitch if the first textField that becomes active is one with the custom picker as inputView , but if the user interact before with a textField with the normal keyboard as inputView the glitch do not appear. 在将UITextField类的外观代理设置为对inputView使用暗模式之后,如果第一个变为活动的textField是自定义选择器作为inputView ,但是如果用户之前使用普通键盘与textField进行交互,则我们会inputView相同的故障作为inputView ,毛刺不会出现。

In our particular case adding this hack to the wrapper view fix the issue: 在我们的特定情况下,将此hack添加到包装器视图可解决此问题:

- (void)willMoveToWindow:(UIWindow *)newWindow
{
    [super willMoveToWindow:newWindow];

    if (newWindow) {
        self.pickerView.backgroundColor = self.colorPalette.inputViewBackgroundColor;
    }
}

We also tried setting the color in the willMoveToSuperview: method but that does not work. 我们还尝试在willMoveToSuperview:方法中设置颜色,但这不起作用。

write your background color change code inside:- 在里面写下你的背景颜色变化代码: -

func textFieldDidBeginEditing(_ textField: UITextField) { 

    if textField == self.tourtextfieldtextField {
        //for background color of picker
        datePickerView.backgroundColor = .red
        //text color of picker
        datePickerView.setValue( colorLiteral(red: 0, green: 0.5529411765, blue: 0.537254902, alpha: 1), forKeyPath: "textColor")
        datePickerView.setValue(0.8, forKeyPath: "alpha")
    }
}

swift 4.2 / xcode 10: swift 4.2 / xcode 10:

The background color need to be set in the main thread: 需要在主线程中设置背景颜色:

DispatchQueue.main.async {
keywordPicker.backgroundColor = myTextViewBackgroundColor
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM