使用UIAppearance自定义iOS UIDatepicker

问题描述 投票:8回答:8

UNIQLO's new alarm app有一个自定义UIDatePicker

我想创建自己的自定义UIDatePicker

我试图改变DatePicker的外观,但在UI_APPEARANCE_SELECTOR中寻找UIDatePicker什么都不返回。

这意味着它不可能根据文档更改任何值:

To support appearance customization, a class must conform to the UIAppearanceContainer protocol and relevant accessor methods must be marked with UI_APPEARANCE_SELECTOR.

怎么能改变我的UIDatePicker的外观?

iphone ios ios5 uidatepicker uiappearance
8个回答
25
投票

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;
}

看一下这个。


4
投票

在您的实现文件中添加此方法

-(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的所有子视图,只需选择任何索引并逐个修改它们

修改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”的背景颜色


3
投票

在iOS 5中引入了UIAppearance协议,它允许您自定义多个UI元素。 UIDatePicker恰好符合此协议,因此这可能是最简单的方法。也就是说,如果您愿意仅支持iOS 5用户。马修的选择可能是下一个最好的选择,它也适用于较旧的iOS版本。


3
投票

改变UIDatePicker出现的方式似乎很难。

在我看来,你提供的例子是一个简单的UIPickerView的定制,有两列,可能是一个模拟的无限滚动(正如Apple在日期选择器中所做的那样,它很容易实现)。

您可以通过UIDatePicker代理更改UIAppearance中的一小部分,如下例所示:

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;

在委托上的方法,您可以使选择器视图的单个元素以您想要的方式显示。然后,您将必须适当地处理数据源,以使选择器的两个组件几乎无限。


0
投票

您可以使用两个UIScrollViews自己实现一个,一个用于小时,一个用于分钟。你只需要弄清楚每个scrollview停止移动时的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。您需要自己制作自定义图稿。


0
投票

要更改日期选择器颜色:

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

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

创建类别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

这将在您的应用程序中的其他位置设置字体(用您的字体替换系统字体)。


-1
投票

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


-1
投票

2 UITableViews的组合是要走的路。

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

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

}

在获取位置时,只需使用以下方法将表格滚动到中心即可

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

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.