/** * Create a time picker*/- (void) Createpickerview {self.datepicker=[[Uidatepicker alloc] init]; _datepicker.backgroundcolor=[Uicolor Whitecolor]; //set to display only Chinese[_datepicker Setlocale:[nslocale Localewithlocaleidentifier:@"ZH-CN"]]; //Set display date only_datepicker.datepickermode =uidatepickermodedate; //Call the time picker when the cursor moves to a text boxSelf.birthTextField.inputView =_datepicker; //Create a tool barUitoolbar *toolbar =[[Uitoolbar alloc] init];
Setting the background color Toolbar.bartintcolor=[Uicolor Whitecolor]; Toolbar.frame= CGRectMake (0,0, Screen_width, -); //Add a button to a toolbarUibarbuttonitem *cancleitem = [[Uibarbuttonitem alloc] Initwithtitle:@"Cancel"Style:uibarbuttonitemstyleplain target:self Action: @selector (cancel)]; Uibarbuttonitem*spaceitem =[[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace target:self Action:nil]; //set the width of the uibarbuttonsystemitemfixedspaceSpaceitem.width = Screen_width- -; Uibarbuttonitem*sureitem = [[Uibarbuttonitem alloc] Initwithtitle:@"Confirm"Style:uibarbuttonitemstyleplain target:self Action: @selector (sure)];
Set Font Color Toolbar.tintcolor=[Uicolor Redcolor]; Toolbar.items=@[cancleitem, Spaceitem, Sureitem]; //set the secondary view of the text input box keyboardSelf.birthTextField.inputAccessoryView =ToolBar;}/** * Click the Cancel button * * @param datePicker*/- (void) Cancel {}/** * Click OK button * * @param datePicker*/- (void) sure {nsstring*date = [NSString stringWithFormat:@"%@", _datepicker.date]; NSString*birth = [Date Substringtoindex:Ten]; Self.birthTextField.text=birth; [_birthtextfield Resignfirstresponder];}
The effect is as follows:
DatePicker Custom--IOS