我正在尝试创建一个UITextField
显示日期选择器而不是键盘的自定义。我已经将故事板中的文本字段更改为使用 DateTextField 自定义类,但是,仍然显示键盘而不是日期选择器。请注意,文本字段位于UITableviewCell
. 另外,我不确定这是否是创建工具栏的正确方法:
self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.superview.bounds.size.width, 44)];
@interface DateTextField : UITextField
@property (strong, nonatomic) UIDatePicker *datePicker;
@property (strong, nonatomic) UIToolbar *datePickerToolbar;
@end
@implementation DateTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.superview.bounds.size.width, 44)];
[self.datePickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(setDatePickerValue:)];
[self.datePickerToolbar setItems:[[NSArray alloc] initWithObjects:extraSpace, doneButton, nil]];
self.inputView = self.datePicker;
self.inputAccessoryView = self.datePickerToolbar;
}
return self;
}
@end