如何自定义UIDatePicker并更改文本颜色和背景颜色?

6
2个回答

13

@AziCode

如果您想获得准确的颜色,请在您的 "func updateDatePicker()" 中添加以下行,并使用 UiColor 的 RGB 颜色编码。

targetedDatePicker.backgroundColor = UIColor.blueColor()
targetedDatePicker.setValue(UIColor.greenColor(), forKeyPath: "textColor")
targetedDatePicker.setValue(0.8, forKeyPath: "alpha")

请参考以下链接和示例代码,了解相关示例:

http://blog.deeplink.me/post/81386967477/how-to-easily-customize-uidatepicker-for-ios

我能在iOS 7中更改UIDatePicker的字体颜色吗?


谢谢,背景颜色的代码运行得很好。是否有类似的解决方案来改变文本颜色(使用Swift而不是Objective-C)? - AziCode
@AziCode 看看我的回答,它包含改变文本颜色的代码。 - Max
1
代码有问题,只有在更改日期时颜色才会改变。然后你会得到绿色的日期和黑色的时间。你觉得有没有办法让整行都变成绿色呢? - AziCode

3

通常我有很好的想法,但只是改变字体颜色:

[self.datePicker setValue:[UIColor whiteColor] forKeyPath:@"textColor"];
    SEL selector = NSSelectorFromString( @"setHighlightsToday:" );
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature :
                                [UIDatePicker
                                 instanceMethodSignatureForSelector:selector]];
    BOOL no = NO;
    [invocation setSelector:selector];
    [invocation setArgument:&no atIndex:2];
    [invocation invokeWithTarget:self.datePicker];

谢谢,但问题有Swift标签,而您的答案是用Objective-C编写的。 - AziCode
抱歉,我没有注意到。 - Genevios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接