当您点击dateTextField
时,它会显示datePicker
toolBar
我将doneButton
添加到toolBar
,但doneButton
无法点击
我该如何解决?
@property (nonatomic, retain) UIDatePicker* datePicker;
@property (nonatomic, retain) UIToolBar* toolBar;
@property (nonatomic, retain) UITextField* dateTextField;
self.datePicker = [[UIDatePicker alloc]init];
self.datePicker.datePickerMode = UIDatePickerModeTime;
self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, -40, 320, 40)];
[self.datePicker addSubview:self.toolBar];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closePicker)];
NSArray* barItems = [NSArray arrayWithObjects:doneButton, nil];
[self.toolBar setItems:barItems animated:YES];
self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 5, 100, 25)];
self.dateTextField.delegate = self;
self.dateTextField.inputView = self.datePicker;
[self.view addSubview:self.dateTextField];
谢谢。
答案 0 :(得分:0)
不要将工具栏添加为日期选择器的子视图。而是将dateTextField的inputAccessoryView属性设置为工具栏(使用工具栏的常规框架)。像这样:
@property (nonatomic, retain) UIDatePicker* datePicker;
@property (nonatomic, retain) UIToolBar* toolBar;
@property (nonatomic, retain) UITextField* dateTextField;
self.datePicker = [[UIDatePicker alloc]init];
self.datePicker.datePickerMode = UIDatePickerModeTime;
self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closePicker)];
NSArray* barItems = [NSArray arrayWithObjects:doneButton, nil];
[self.toolBar setItems:barItems animated:YES];
self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(90, 5, 100, 25)];
self.dateTextField.delegate = self;
self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.toolbar;
[self.view addSubview:self.dateTextField];
另请查看此Apple文章了解更多信息: