UIDatePicker with “done” button

后端 未结 5 1326
闹比i
闹比i 2021-01-01 20:34

I need to add a \"done\" button to my date picker like in this image:

\"enter

5条回答
  •  轮回少年
    2021-01-01 21:34

    The reference image of your question is showing that the app is using UIToolbar and i will assume that when tapping on date text field the datepicker with done button appears.And for this in some.h file

    /*****keyboard Done button   ***/
    
    
    
    #import 
    
    @interface ViewController : UIViewController
    {
       IBOutlet UIDatePicker *picker1;
        IBOutlet UITextField *txtFld;
    
    }
    @property (nonatomic, retain) UIToolbar *keyboardToolbar;
    
    @end
    

    in some.m file

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
     picker1=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];//frames are just for demo
     [txtFld setInputView:picker1];
    }
    
    - (void)keyboardWillShow:(NSNotification *)notification
    {
        if(keyboardToolbar == nil) {
            keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 410, 320, 44)] ;
            [keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
            [keyboardToolbar sizeToFit];
    
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.4];
    
            UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
            UIBarButtonItem *doneButton1 =[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)];
            NSArray *itemsArray = [NSArray arrayWithObjects:flexButton,doneButton1, nil];
    
            [keyboardToolbar setItems:itemsArray];
    
    
            [txtFld setInputAccessoryView:keyboardToolbar];
            [self.view addSubview:keyboardToolbar];
            [UIView commitAnimations];
        }
    }
    
    
    
    -(void)resignKeyboard {
    
        [keyboardToolbar removeFromSuperview];
        [txtFld resignFirstResponder];
    ///do nescessary date calculation here
    
        }
    

    enter image description here

提交回复
热议问题