使用 UIAppearance 自定义 iOS UIDatepicker [英] Custom iOS UIDatepicker using UIAppearance

查看:16
本文介绍了使用 UIAppearance 自定义 iOS UIDatepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何改变我的 UIDatePicker 的外观?

解决方案

API 没有提供执行此操作的方法.您可以使用 UIPickerView 而不是使用 UIDatePicker 自己制作一个非常令人信服的副本.

由于 UIDatePicker 或 UIPickerView 没有 UI_APPEARANCE_SELECTOR,即使您无法将 UIDatePicker 内容的外观更改为其 UIControl 并且没有任何委托,因此它具有其本机外观,而在 UIPickerView 的情况下,您可以更改其内容'外观类似于 UITableView.

#pragma mark -#pragma mark UIPicker 代表 &数据源- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {返回 2;}//返回每个组件中的行数..- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {返回 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];[标签 setBackgroundColor:[UIColor clearColor]];[标签 setTextColor:[UIColor blueColor]];[标签 setFont:[UIFont boldSystemFontOfSize:40.0]];[label setText:[NSString stringWithFormat:@"%d",row]];返回标签;}

看看这个.

UNIQLO's new alarm app has a custom UIDatePicker:

And I want to create my own custom UIDatePicker.

I tried to change the appearance of the DatePicker, but looking for UI_APPEARANCE_SELECTOR in the UIDatePicker returns nothing.

Meaning that its not possible to change any values, as per the docs:

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

How can change my UIDatePicker's appearance?

解决方案

The API does not provide a way to do this. You can make a pretty convincing replica yourself using a UIPickerView rather than using UIDatePicker.

As the UIDatePicker or UIPickerView don't have the UI_APPEARANCE_SELECTOR and even you can't change UIDatePicker contents' appearance as its UIControl and not having any delegate so it has its native appearance whereas in case of UIPickerView you can change its contents' appearance similar as in 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;
}

Check this out.

这篇关于使用 UIAppearance 自定义 iOS UIDatepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆