带有Actionsheet的iPhone DatePicker

时间:2012-01-19 16:56:58

标签: iphone objective-c ios datepicker

我正在使用此代码显示datePicker。单击textField时,会出现。但是,“关闭”按钮会覆盖DatePicker。你知道有什么方法可以将它降低一点吗?提前谢谢。

-(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];



}

3 个答案:

答案 0 :(得分:8)

如果您想在UIDatePicker成为第一响应者时显示UITextField,则应将日期选择器设置为文本字段inputView。然后,当您点击该字段时,日期选择器将向上滑动而不是键盘。

答案 1 :(得分:1)

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

7.0f 30.f 更改为更多正值。然后尝试。

7.0f值是Button // increase it to shift the button lower

的origin.y

30.f value是按钮// increase it by the same amount you increase origin to keep proportional height

的size.height

答案 2 :(得分:0)

我自己解决了这个问题;

我改变了这一行:

  pickerView1 = [[UIDatePicker alloc] init];

进入这个:

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