2009-09-23 36 views

回答

4

我想你是在處理iPhone SDK?可能有一些其他的框架使用這個名字,所以也許你可以添加你的標籤來包含uikit,cocoa-touch或其他東西。

無論如何,您可以將UIPickerView實例的showsSelectionIndicator設置爲NO,因此它隱藏了選擇器。然後,您可以使用調整後的選擇樣式創建一個新視圖,並將其添加到UIPickerView上方的超級視圖中。

// 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]; 

黑客UI元素本身將需要更多的工作,這也必須工作。

18

也許它並不完全適合於這個問題的答案,在搭載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]; 

UPDATE

先前的代碼有效,但馬馬虎虎。這裏簡單的子類UIPickerView

斯威夫特:

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 

,你可以直接在故事板

感謝羅斯Barbish設置選擇顏色 - 「與iOS 9.2和XCode 7.2 2015年12月8日發佈,此選擇視圖的高度爲0.666666666666667「。

UPDATE:

這是修復的問題與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 
      } 
     } 
    } 
} 

感謝德米特里克洛奇科夫,我會盡量找到一些更好的解決方案。

+0

這確實爲我工作,但有沒有保證,這將在未來不打破,如果蘋果使該pickerView任何更改。 – Sid 2014-11-15 23:19:38

+0

謝謝!這是改變選擇指示器顏色的唯一真實方法。快速工作。 – ObjectiveTC 2015-07-31 09:59:11

+0

這個作品很棒! – 2015-09-23 08:20:16

0

您也可以只更改選擇指示器的背景顏色
只需加選擇指示器上面一個UIView(它會成爲你的重疊視圖),設置它的alpha值低(取決於你,但我喜歡我的覆蓋看起來透明),給它一個背景色你很好走。

考慮這一點,

var overlayOnPicker:UIView = UIView(frame: CGRectMake(1, 68, mypicker.frame.width, 26)) 
// Adds a layer on the selection indicator 

和DO把的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 
9

下面是對vsilux的答案的一種改進,以UIPickerView的簡單類別的形式,而不需要子類UIPickerView。

斯威夫特:

private var selectorColorAssociationKey: UInt8 = 0 

extension UIPickerView { 

    @IBInspectable var selectorColor: UIColor? { 
     get { 
      return objc_getAssociatedObject(self, &selectorColorAssociationKey) as? UIColor 
     } 
     set(newValue) { 
      objc_setAssociatedObject(self, &selectorColorAssociationKey, newValue, 
       objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) 
     } 
    } 

    public override func didAddSubview(subview: UIView) { 

     super.didAddSubview(subview) 
     if let color = selectorColor { 
      if subview.bounds.height < 1.0 { 
       subview.backgroundColor = color 
      } 
     } 
    } 

}