user973067
user973067

Reputation: 337

iPhone DatePicker with Actionsheet

I am using this code to display a datePicker. When a textField was clicked, it appears. However, The "close" button overwraps the DatePicker. Do you know any way to move it down a little bit? thanks in advance.

-(IBAction)acsheet:(id)sender{

actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                          delegate:nil
                                 cancelButtonTitle:nil
                            destructiveButtonTitle:nil
                                 otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

//CGRect pickerFrame = CGRectMake(0, 45, 0, 0);
pickerView1 = [[UIDatePicker alloc] init];
pickerView1.datePickerMode = UIDatePickerModeDate;  



[actionSheet addSubview:pickerView1];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];    
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

[pickerView1 addTarget:self
             action:@selector(updateLabel:)
     forControlEvents:UIControlEventValueChanged]; 
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)] ;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;

[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];



}

Upvotes: 1

Views: 3040

Answers (3)

user973067
user973067

Reputation: 337

I've worked out by myself;

i've changed this line:

  pickerView1 = [[UIDatePicker alloc] init];

into this:

pickerView1 = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 450)];

Upvotes: 0

Dave DeLong
Dave DeLong

Reputation: 243156

If you want a UIDatePicker to appear when a UITextField becomes the first responder, then you should set the date picker to be the textfield's inputView. Then when you tap on the field, the date picker will slide up instead of the keyboard.

Upvotes: 8

Shubhank
Shubhank

Reputation: 21805

closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);

change the 7.0f and 30.f to more positive values.and then try.

7.0f value is the origin.y of Button // increase it to shift the button lower

30.f value is the size.height of button// increase it by the same amount you increase origin to keep proportional height

Upvotes: 1

Related Questions