在我的项目中,我有一个UIButton和一个UIView,在视图中我添加了UIDatePicker。当我按下按钮时,日期选择器打开。如何解雇日期选择器?
答案 0 :(得分:1)
我希望以下代码能解决您的问题。这解雇了日期选择器" Done"按钮点击以及在叠加视图上点击外部:
#import "ViewController.h"
#define kOverlayviewTag 1
#define kDatepickerTag 2
#define kToolbarTag 3
#define kToolbarHeight 44
#define kDatepickerHeight 216
#define kGenericWidth 320
#define kInvisibleAlpha 0
#define kTransparentAlpha 0.5
#define kOpaqueAlpha 1
@interface ViewController ()
-(IBAction)buttonTapped:(id)sender;
@end
@implementation ViewController
-(void)buttonTapped:(id)sender
{
if ([self.view viewWithTag:kOverlayviewTag]) {
return;
}
UIView *overlayView = [[UIView alloc] initWithFrame:self.view.bounds];
overlayView.alpha = kInvisibleAlpha;
overlayView.backgroundColor = [UIColor blackColor];
overlayView.tag = kOverlayviewTag;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)];
[overlayView addGestureRecognizer:tapGesture];
[self.view addSubview:overlayView];
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+kToolbarHeight, kGenericWidth, kDatepickerHeight)];
datePicker.tag = kDatepickerTag;
[datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, kGenericWidth, kToolbarHeight)];
toolBar.tag = kToolbarTag;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-kDatepickerHeight-kToolbarHeight, kGenericWidth, kToolbarHeight);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-kDatepickerHeight, kGenericWidth, kDatepickerHeight);
[UIView animateWithDuration:0.5 animations:^{
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
overlayView.alpha = kTransparentAlpha;
}];
}
- (void)dismissDatePicker:(id)sender {
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, kGenericWidth, kToolbarHeight);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+kToolbarHeight, kGenericWidth, kDatepickerHeight);
[UIView animateWithDuration:0.5 animations:^{
[self.view viewWithTag:kOverlayviewTag].alpha = kInvisibleAlpha;
[self.view viewWithTag:kDatepickerTag].frame = datePickerTargetFrame;
[self.view viewWithTag:kToolbarTag].frame = toolbarTargetFrame;
} completion:^(BOOL finished) {
[[self.view viewWithTag:kOverlayviewTag] removeFromSuperview];
[[self.view viewWithTag:kToolbarTag] removeFromSuperview];
[[self.view viewWithTag:kDatepickerTag] removeFromSuperview];
}];
}
- (void)changeDate:(UIDatePicker *)sender {
NSLog(@"New Date: %@", sender.date);
}
中的UIAnimation块更新
答案 1 :(得分:0)
我通常做的是在UIDatePicker的顶部添加一个工具栏。在工具栏中,我添加了一个按钮,该按钮调用方法在按下时关闭日期选择器。
您还可以在UIDatePicker实例下添加UIView实例,并实现touchesBegan方法来关闭UIDatePicker。这样,当用户点击日期选择器之外时,它会调用关闭datepicker的touchesBegan方法。