【问题标题】:UIActionSheet on iPad frame too smalliPad 框架上的 UIActionSheet 太小
【发布时间】:2011-12-19 22:37:10
【问题描述】:

我试图在 iPad 上显示一个 UIActionSheet 并在其中显示一个 UIPickerView,我有适用于 iPhone 的等效代码,所以我的 UIPickerView 委托和数据源的东西可以工作,但是当我使用 @ 时在 iPad 上987654324@ 结果UIPopover/UIActionSheet 太小了,我似乎无法设置框架大小,也没有显示任何按钮。

我不知道这是因为它们超出了界限还是发生了其他事情。这是我删除所有非必要代码(iPhone 等)后代码的样子。有谁知道我做错了什么,有没有人知道任何例子。

CGRect thePickerFrame = CGRectMake(0, 0, 320.0, 485.0);
UIPickerView * thePickerView = [[UIPickerView alloc] initWithFrame:thePickerFrame];

[pickerActionSheet release], pickerActionSheet =
        [[UIActionSheet alloc] initWithTitle:@"Choose" delegate:self 
                               cancelButtonTitle:@"Cancel"
                               destructiveButtonTitle:nil
                               otherButtonTitles:@"Next", nil];
thePickerView.showsSelectionIndicator = YES;
thePickerView.dataSource = self;
thePickerView.delegate = self;

[pickerActionSheet addSubview:thePickerView];
[thePickerView selectRow:0 inComponent:0 animated:NO];
[thePickerView release];

[pickerActionSheet showFromRect:currentTextField.bounds
                             inView:currentTextField animated:NO];
pickerActionSheet.frame = thePickerFrame;

【问题讨论】:

  • 我只是做了一些实验,看看何时调用 - [UIActionSheet setFrame:]。我似乎在上面被称为另一个由 iOS 发起的事件循环的事件循环之后 - [UIActionSheet setFrame:] 被调用并显示的小框架。
  • 使用此功能在 iPad 上使用 popover 不是更好吗? AFAIK ActionSheet 仅适用于按钮。

标签: objective-c ios cocoa-touch ipad


【解决方案1】:

我认为 UIActionSheet 无法调整大小,尝试在代码中注释 [pickerActionSheet addSubview:thePickerView]; 行,您会发现 ActionSheet 非常适合按钮。

我会推荐 UIPopoverController 和自定义 UIViewController。像这样的:

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle = UIBarStyleDefault;

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(BACK_ACTION:)];
UIBarButtonItem *chooseButton = [[UIBarButtonItem alloc] initWithTitle:@"Choose" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(NEXT_ACTION:)];
UIBarButtonItem *fixed1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fixed2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

[toolbar setItems:[NSArray arrayWithObjects:cancelButton, fixed1, chooseButton, fixed2, nextButton, nil]];

UIPickerView    *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
CGRect thePickerFrame = thePickerView.frame;
thePickerFrame.origin.y = toolbar.frame.size.height;
[thePickerView setFrame:thePickerFrame];


UIView *view = [[UIView alloc] init];
[view addSubview:thePickerView];
[view addSubview:toolbar];

UIViewController *vc = [[UIViewController alloc] init];
[vc setView:view];
[vc setContentSizeForViewInPopover:CGSizeMake(320, 260)];

popover = [[UIPopoverController alloc] initWithContentViewController:vc];

thePickerView.showsSelectionIndicator = YES;
thePickerView.dataSource = self;
thePickerView.delegate = self;

[thePickerView selectRow:0 inComponent:0 animated:NO];

[popover presentPopoverFromRect:currentTextField.bounds inView:currentTextField permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

其中 popover 是在 .h (UIPopoverController *popover;) 中声明的类变量。

顺便说一下,我用的是ARC,所以代码里没有release。

【讨论】:

    【解决方案2】:

    我设法用一种愚蠢的方式让它工作。

    供您参考:
    设置 PickerView。

    UIActionSheet 的 宽度无法以某种方式调整,因此必须相应调整pickerView。高度方面,您可以调整 actionSheet 标题中 "\n" 的数量。

    UIDatePicker * pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 270, 220)];
    pickerView.datePickerMode = UIDatePickerModeDate; 
    
    UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];    
    
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    [actionSheet addSubview:pickerView];
    [actionSheet showFromRect:button.frame inView:button.superview animated:YES];
    [actionSheet release];
    

    Set Frame or Bound 对我不起作用。

    [actionSheet setBounds:pickerView.frame];   
    [actionSheet setFrame:pickerView.frame];
    



    同意使用 UIPopoverController,但不知道为什么,当我将 UIDatePickerView 放入 popover 时出现了严重的 UI 延迟(弹出几乎 1 秒的延迟),我找不到根本原因。所以不得不回退到上述方法。


    快乐编码。

    【讨论】:

    • 虽然 hacky,但这至少在通用应用程序中很容易实现。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多