说一下textField两个属性:
// Presented when object becomes first responder. If set to nil, reverts to following responder chain. If
// set while first responder, will not take effect until reloadInputViews is called.
@property (nullable,readwrite,strong)UIView *inputView;
@property (nullable,readwrite,strong)UIView *inputAccessoryView;
从苹果官方解释来看就是你获得第一响应的时候,弹出的InputViews改变。
-(void)setBrithDayInputView{
datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0,0,ScreenWidth,200)];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePickeraddTarget:selfaction:@selector(dateChanged)forControlEvents:UIControlEventValueChanged];
accessoryView = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,ScreenWidth,40)];
UIBarButtonItem *flex = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil];
UIBarButtonItem *right = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(done)];
accessoryView.items =@[flex,right];
self.brithdayInputView.inputView = datePicker;
self.brithdayInputView.inputAccessoryView = accessoryView;
}
-(void)dateChanged{
NSDateFormatter *dateFormat=[[NSDateFormatteralloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString * date =[dateFormatstringFromDate:datePicker.date];
self.brithdayInputView.text = date;
}
-(void)done{
[self.brithdayInputViewresignFirstResponder];
[selfdateChanged];
}
再简单的说下小细节吧
inputAccessoryView 是键盘的附属视图注意frame
toolbarItem 设置位置没有用,记得用弹簧调整你想要的位置。