如何在横向模式下在iPad中使用UIDatePicker?

时间:2013-08-21 05:03:09

标签: ios uidatepicker

我在通用应用上展示了UIDatePicker。在iPhone上它显示很好,在iPad上它只显示底部。 这是我的代码:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        [aTextField resignFirstResponder];
        pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

        UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 64, 0,0)];
        pickerView.datePickerMode = UIDatePickerModeDate;
        pickerView.hidden = NO;
        pickerView.date = [NSDate date];
        [pickerView addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];

        UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,1024,64)];
        pickerToolbar.barStyle = UIBarStyleBlackOpaque;
        [pickerToolbar sizeToFit];

        NSMutableArray *barItems = [[NSMutableArray alloc] init];
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];

        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
        [barItems addObject:doneBtn];

        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
        [barItems addObject:cancelBtn];

        [pickerToolbar setItems:barItems animated:YES];
        [pickerViewPopup addSubview:pickerToolbar];
        [pickerViewPopup addSubview:pickerView];
        [pickerViewPopup showInView:self.view];
        [pickerViewPopup setBounds:CGRectMake(0,0,1024, 768)];

    }

1 个答案:

答案 0 :(得分:0)

我使用了该代码并且正在使用

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {
            [aTextField resignFirstResponder];
            UIViewController *popoverContent = [[UIViewController alloc] init];
            UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
            popoverView.backgroundColor = [UIColor whiteColor];
            UIDatePicker *datePicker=[[UIDatePicker alloc]init];
            datePicker.frame = CGRectMake(0, 0, 320, 300);
            datePicker.datePickerMode = UIDatePickerModeDate;
            datePicker.hidden = NO;
            datePicker.date = [NSDate date];
            [datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
           [popoverView addSubview:datePicker];
            popoverContent.view = popoverView;
            popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 200);
            UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
            [popoverController presentPopoverFromRect:CGRectMake(0, 0, 1150,480) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}