Search code examples
iosuidatepickeruitoolbarlandscape-portrait

Resizable Toolbar with bar button on UIDate Picker


I have used date picker at place of keypad.And UI bar Button With ok,cancel and Flexible space. But It is not responsive when i change Portrait to LandScape mode.

Tool Bar:-

UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    toolbar.barStyle=UIBarStyleBlackTranslucent;


    cancelBtn=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:nil];
    flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"OK"style:UIBarButtonItemStyleDone target:self action:nil];

    [toolbar setItems:[NSArray arrayWithObjects:cancelBtn,flexible,doneBtn,nil]];


     [toolbar sizeToFit];
    _txt_dob.inputAccessoryView=datePicker;

UIDate Picker:-

datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [_txt_dob setInputView:datePicker];
    [datePicker addSubview:toolbar];

Portrait Mode

LandScape Mode of Simulator. LandScape Mode


Solution

  • Do not initialize your UIToolBar with frame. Instead use NSLayoutConstraint between your UIToolBar and its parent view to stretch it all the way. That way it will respond well to orientation changes.