我有一个UIPicker,我想改变选择器的颜色,是否可以改变选择器的颜色?
也许这并不完全适合回答这个问题,在iOS 7及更高版本中,您可以通过以下方式自定义颜色:
在委托方法中
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
添加以下内容
[[pickerView.subviews objectAtIndex:1] setBackgroundColor:NEEDED_COLOR];
[[pickerView.subviews objectAtIndex:2] setBackgroundColor:NEEDED_COLOR];
更新
之前的代码可以工作,但效果一般。这里提供了用于UIPickerView的简单子类。
Swift:
class RDPickerView: UIPickerView
{
@IBInspectable var selectorColor: UIColor? = nil
override func didAddSubview(subview: UIView) {
super.didAddSubview(subview)
if let color = selectorColor
{
if subview.bounds.height <= 1.0
{
subview.backgroundColor = color
}
}
}
}
Objective-C:
@interface RDPickerView : UIPickerView
@property (strong, nonatomic) IBInspectable UIColor *selectorColor;
@end
@implementation RDPickerView
- (void)didAddSubview:(UIView *)subview
{
[subview didAddSubview:subview];
if (self.selectorColor)
{
if (subview.bounds.size.height <= 1.0)
{
subview.backgroundColor = self.selectorColor;
}
}
}
@end
你可以直接在故事板中设置选择器的颜色。
感谢Ross Barbish - “随着iOS 9.2和XCode 7.2于2015年12月8日发布,此选择视图的高度为0.666666666666667”。
更新:
这是解决iOS 10问题的方法,不是很好但是有效。 :/
class RDPickerView: UIPickerView
{
@IBInspectable var selectorColor: UIColor? = nil
override func didAddSubview(_ subview: UIView) {
super.didAddSubview(subview)
guard let color = selectorColor else {
return
}
if subview.bounds.height <= 1.0
{
subview.backgroundColor = color
}
}
override func didMoveToWindow() {
super.didMoveToWindow()
guard let color = selectorColor else {
return
}
for subview in subviews {
if subview.bounds.height <= 1.0
{
subview.backgroundColor = color
}
}
}
}
谢谢Dmitry Klochkov,我会尝试找到更好的解决方案。
以下是对vsilux答案的改进,以简单类别的形式提供给UIPickerView,无需子类化UIPickerView。
(截至2018年11月2日最新答案)
Swift 4,Xcode 10:
@IBInspectable var selectorColor: UIColor? {
get {
return objc_getAssociatedObject(self, &selectorColorAssociationKey) as? UIColor
}
set(newValue) {
objc_setAssociatedObject(self, &selectorColorAssociationKey, newValue,
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
open override func didAddSubview(_ subview: UIView) {
super.didAddSubview(subview)
if let color = selectorColor {
if subview.bounds.height < 1.0 {
subview.backgroundColor = color
}
}
}
我想你在处理iPhone SDK?可能有一些其他使用这个名称的框架,所以你可以添加标签来包括uikit、cocoa-touch或其他内容。
无论如何,你可以将UIPickerView实例的showsSelectionIndicator
设置为NO,这样它就会隐藏选择器。然后,你可以创建一个新视图,调整选择样式,并将其添加到UIPickerView上面的superview中。
// Some sample code, but you can do this in IB if you want to
_pickerView = [[UIPickerView alloc] init];
_pickerView.showsSelectionIndicator = NO;
[_pickerView sizeToFit];
[self.view addSubview:_pickerView];
UIImage *selectorImage = [UIImage imageNamed:@"selectorImage.png"]; // You have to make it strechable, probably
UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
customSelector.frame = CGRectZero; // Whatever rect to match the UIImagePicker
[self.view addSubview:customSelector];
[customSelector release];
var overlayOnPicker:UIView = UIView(frame: CGRectMake(1, 68, mypicker.frame.width, 26))
// Adds a layer on the selection indicator
请将CGRect的X值设置为1(记住,它是一个框架,所以它将根据父视图的坐标系进行定位)
overlayOnPicker.backgroundColor = UIColor.whiteColor()
overlayOnPicker.alpha = 0.2
myDatePicker.addSubview(overlayOnPicker)
// You have to add the overlayOnPicker view as a subview to the Date Picker.
// myDatePicker is the UIDatePicker that I declared earlier in my code
您还可以对UIPickerView进行子类化,并添加以下自定义内容:
import UIKit
class CustomPickerView: UIPickerView {
init() {
super.init(frame: .zero)
}
override func layoutSubviews() {
super.layoutSubviews()
subviews.forEach { $0.backgroundColor = .clear }
}
override init(frame: CGRect) {
fatalError("init(coder:) has not been implemented")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
picker.subviews[1].backgroundColor = UIColor.red
- multitudes