我有一個UIPicker,我想更改選擇器的顏色。是否有可能改變選擇器的顏色?如何更改UIPickerView選擇器的顏色
4
A
回答
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
您也可以只更改選擇指示器的背景顏色。
只需加選擇指示器上面一個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
}
}
}
}
相關問題
- 1. 更改UIPickerView主體顏色
- 2. 如何更改UITabbar選擇的顏色?
- 3. 如何更改UITabBar選擇顏色
- 4. 根據顏色選擇器更改圖像的顏色
- 5. 如何從虹膜顏色選擇器獲取更改的顏色代碼
- 6. UIPickerView行圖像選擇高亮顏色
- 7. 使用顏色選擇器更改圖層樣式顏色
- 8. 使用顏色選擇器更改網站背景顏色
- 9. 如何更改JavaFX中顏色選擇器的圖標大小?
- 10. 如何更改從選擇器的textView顏色
- 11. 如何更改jQuery日期選擇器的單元格顏色
- 12. 如何更改內部選擇器內部漸變的顏色?
- 13. Iphone UIPickerView:更改選擇器高度
- 14. Kendo UI Editor - 更改字體顏色選擇器和背景顏色選擇器的默認顏色
- 15. 如何在iOS中將UIPickerView文本顏色更改爲白色
- 16. 如何更改wordpress顏色選擇器按鈕文本?
- 17. 如何使用顏色選擇器更改邊框背景
- 18. 如何在選擇時將UITableViewCell的顏色更改爲藍色
- 19. 更改選擇的視圖顏色
- 20. 更改選擇偏好的顏色
- 21. 更改ListView的背景選擇顏色?
- 22. 更改JavaFX TreeTableView的選擇顏色
- 23. 更改ListBoxItem的選擇顏色
- 24. 更改vim選擇的文本顏色
- 25. 更改zedgraph選擇的曲線顏色
- 26. 更改文字的顏色上選擇
- 27. 更改選擇框的邊框顏色
- 28. 更改AS3中的TextField選擇顏色
- 29. 更改WPF ListViewItem的選擇顏色
- 30. 錶行選擇的顏色更改JavaFX
這確實爲我工作,但有沒有保證,這將在未來不打破,如果蘋果使該pickerView任何更改。 – Sid 2014-11-15 23:19:38
謝謝!這是改變選擇指示器顏色的唯一真實方法。快速工作。 – ObjectiveTC 2015-07-31 09:59:11
這個作品很棒! – 2015-09-23 08:20:16