在iPad上,在UIActionSheet中使用UIDatePicker

15

在我的iPhone应用程序中,我有一个放置在UIActionSheet中的UIDatePicker。它显示正常。现在我正在设置iPad版本的应用程序,但是当在iPad上查看相同的UIActionSheet时,它会出现一个空白框。

这是我使用的代码:

UIDatePicker *datePickerView = [[UIDatePicker alloc] init];
    datePickerView.datePickerMode = UIDatePickerModeDate;

    self.dateActionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose a Follow-up Date"
                                                   delegate:self cancelButtonTitle:nil 
                                     destructiveButtonTitle:nil otherButtonTitles:@"Done", nil];

    [self.dateActionSheet showInView:self.view];
    [self.dateActionSheet addSubview:datePickerView];
    [self.dateActionSheet sendSubviewToBack:datePickerView];
    [self.dateActionSheet setBounds:CGRectMake(0,0,320, 500)];

    CGRect pickerRect = datePickerView.bounds;
    pickerRect.origin.y = -95;
    datePickerView.bounds = pickerRect;

2
这个问题让我更好地理解了苹果公司希望iPad用户如何使用Popovers和ActionSheets,比官方文档更加清晰明了。非常感谢! - decoy
检查我的答案,它可能对你有用。 - Narayana Rao Routhu
谢谢@Narayana,但这个问题是6年前的 :-) - Chris
@Chris 是的,我的问题也是6年前,我已经用Swift更新了我的答案,所以添加了这个评论。 - Narayana Rao Routhu
请查看此答案:https://dev59.com/uGs05IYBdhLWcg3wQvyq#7343106 - Narayana Rao Routhu
1个回答

21

最终我创建了一个独立的代码段来处理iPad的弹出窗口:

//build our custom popover view
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 344)];
popoverView.backgroundColor = [UIColor whiteColor];

datePicker.frame = CGRectMake(0, 44, 320, 300);

[popoverView addSubview:toolbar];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;

//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);

//create a popover controller
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

//present the popover view non-modal with a
//refrence to the button pressed within the current view
[popoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem 
                          permittedArrowDirections:UIPopoverArrowDirectionAny 
                                          animated:YES];

//release the popover content
[popoverView release];
[popoverContent release];

1
这很好,但是我得到了日期选择器上方的空白白框,这是可以预料的。有人知道我如何利用这个空间吗?我想在这里设置一个标题。 - AbuZubair
@AbuZubair 你可能需要在CGRect上设置一个负值,这样标签就会出现在未使用的空间中 - 这很hacky,但可能会起作用。 :-/ - Norman H

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接