2013-08-21 56 views
0

我在通用應用上顯示UIDatePicker。在iPhone上它顯示得很好,在iPad上它只顯示底部。 這是我的代碼:如何在iPad上使用橫向模式的UIDatePicker?

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

    } 
+0

你需要使用UIPopoverController,而不是動作片在ipad –

+0

好thanx.But的情況下,你有什麼例子? –

+0

嘗試改變這一行[pickerViewPopup setBounds:CGRectMake(0,0,1024,768)]; //將setBounds方法更改爲setFrame – Tendulkar

回答

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]; 
} 
相關問題