使用UIAppearance自定义iOS UIDatepicker

优衣库的新的闹钟应用程序有一个自定义的UIDatePicker

习惯

我想创build我自己的自定义UIDatePicker

我试图改变DatePicker的外观,但在UIDatePicker查找UI_APPEARANCE_SELECTOR 不会返回任何内容

意思是不可能改变任何值,按照文档:

为了支持外观定制,一个类必须符合UIAppearanceContainer协议,相关的访问器方法必须用UI_APPEARANCE_SELECTOR标记。

如何改变我的UIDatePicker的外观?

API不提供这样做的方法。 您可以使用UIPickerView而不是使用UIDatePicker来制作一个相当有说服力的副本。

由于UIDatePicker或UIPickerView没有UI_APPEARANCE_SELECTOR,甚至不能将UIDatePicker内容的外观作为其UIControl进行更改,也没有任何委托,所以它具有其原生外观,而对于UIPickerView,您可以更改其内容的外观在UITableView。

 #pragma mark - #pragma mark UIPicker Delegate & DataSource - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } // returns the # of rows in each component.. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return 100; } //- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component { // return [NSString stringWithFormat:@"%d",row]; //} - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *label= [[[UILabel alloc] initWithFrame:CGRectMake(30.0, 0.0, 50.0, 50.0)] autorelease]; [label setBackgroundColor:[UIColor clearColor]]; [label setTextColor:[UIColor blueColor]]; [label setFont:[UIFont boldSystemFontOfSize:40.0]]; [label setText:[NSString stringWithFormat:@"%d",row]]; return label; } 

在这里输入图像描述

看一下这个。

在你的实现文件中添加这个方法

 -(UIView *)pickerViews{ return ([datePicker.subviews objectAtIndex:0]); } -(void)viewDidLoad{ [super viewDidLoad]; [[self pickerViews].subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%@ --- > %i",obj, idx); }]; } 

当你运行你的应用程序时,它会在你的控制台上显示datePicker的所有子视图,只要select任何索引并逐一修改它们

修改viewDidLoad并插入这段代码

 UIView *background = (UIView *)[[self pickerViews].subviews objectAtIndex:0]; background.backgroundColor = [UIColor clearColor]; UIView *wheel2 = (UIView *)[[self pickerViews].subviews objectAtIndex:4]; wheel2.backgroundColor = [UIColor redColor]; UIView *wheel3 = (UIView *)[[self pickerViews].subviews objectAtIndex:11]; wheel3.backgroundColor = [UIColor redColor]; 

在这种情况下,我改变索引“0”,“4”,“11”

在iOS 5中引入了UIAppearance协议,可以让您自定义多个UI元素。 UIDatePicker碰巧符合这个协议,所以这可能是最简单的方法。 也就是说,如果你愿意只支持iOS 5的用户。 马修的select可能是下一个最好的事情,这也应该与旧的iOS版本。

改变UIDatePicker出现的方式似乎相当困难。

在我看来,你提供的例子是一个简单的UIPickerView有两列,可能是一个模拟的无限滚动(就像苹果在dateselect器中做的那样,而且很容易实现)。

您可以通过UIAppearance代理更改less许的UIDatePicker ,如以下示例所示:

 UIDatePicker *picker = [UIDatePicker appearance]; picker.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.3]; UIView *view; view = [UIView appearanceWhenContainedIn:[UITableView class], [UIDatePicker class], nil]; view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; UILabel *label = [UILabel appearanceWhenContainedIn:[UITableView class], [UIDatePicker class], nil]; label.font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; label.textColor = [UIColor blueColor]; 

在应用程序的开始部分使用这段代码(只需尝试一下)就可以改变dateselect器中几个主要组件的方面。

标签是不可能定制的(并且这个testing代码certificate了这一点); 显然,每次旋转列时都会改变方向,因为它们放在自定义的UITableView控制器中。

要充分改变这些外观,您应该使用Apple的私有API,这最终会导致您的应用被拒绝

如果您只需要一个自定义小时分拣机(如屏幕截图中所示),则只有扩展UIPickerView类或将适当的UIPickerView分配给UIPickerView实例才可以完全自定义外观。

通过

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; 

方法,可以使选取器视图的单个元素以您想要的方式显示。 那么你将不得不妥善处理数据源,使select器的两个组件实际上是无限的。

您可以使用两个UIScrollView自己实现一个,一个小时,一个分钟。 你只需要找出每个滚动视图的contentOffset,当它停止移动的时候,我的头顶就像这样:

 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { // get the offset here } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { // also get the offset here } 

取内容偏移量的值并将其转换为小时值和分钟值,可能是将内容的y位置分别偏移24或60。 您将需要自己制作自定义图片。

要更改dateselect器的颜色:

 [datePickerView setValue:self.pickerLabelColor forKey:@"textColor"]; 

而改变字体我知道的唯一方法是:

创build类别UIFont+System并将此代码放在里面

 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" + (UIFont *)systemFontOfSize:(CGFloat)fontSize { return [UIFont fontWithName:fontName size:fontSize]; } #pragma clang diagnostic pop 

这将在您的应用程序的另一个地方设置字体(用您的字体replace系统字体)。

你应该通过苹果的UICatalog。 它具有可以使用UIDatePicker的所有方法,包括自定义dateselect器方法。 研究这个链接developer.apple.com/library/ios/samplecode/UICatalog/。 他们也实现了标准的开关和后者给出的导航栏。

2 UITableViews的组合是要走的路。

UITableViewUIScrollView的子视图,所以它处理以下事件

 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { } 

并获得位置,只需使用以下方法将表格滚动到中心

 [myTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

希望这可以帮助。