似乎很难改变UIDatePicker 的出现方式。
在我看来,您提供的示例是一个简单的 UIPickerView 的复杂定制,它有两列,可能是一个模拟的无限滚动(就像 Apple 在日期选择器中所做的那样,实现起来非常简单)。
您可以通过UIAppearance 代理对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];
在应用程序启动时使用这段代码(试一试),您只能更改日期选择器的几个主要组件的外观。
标签是不可能自定义的(这个测试代码证明了这一点);显然,每次旋转列时它们的外观都会发生变化,因为它们被放在自定义 UITableView 控制器中。
要完全改变这些外观,您可能应该使用 Apple 的私有 API,这最终会导致您的应用被拒绝。
如果您只需要自定义小时-分钟选择器(如屏幕截图所示),则只能扩展 UIPickerView 类或将适当的委托分配给 UIPickerView 实例,才能实现外观的完全自定义。
通过
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
委托上的方法,您可以使选择器视图的单个元素以您想要的方式显示。然后您必须适当地处理数据源,以使选择器的两个组件实际上是无限的。