2011-11-02 10 views
13

私はUIPickerViewでiPadにUIActionSheetを表示しようとしていますが、私はUIPickerViewの代理人とデータソースの仕事をしていますが、-[UIActionSheet showFromRect:inView:animated:]結果UIPopover/UIActionSheetが小さすぎてフレームサイズを設定できないようで、ボタンも表示されません。iPadフレームの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; 
+0

私は、[UIActionSheet setFrame:]が呼び出されているかどうかを少し実験しました。上記のイベントループの後に、iOSによって開始された別のイベントループと呼ばれる - [UIActionSheet setFrame:]が表示されている小さなフレームで呼び出されているようです。 –

+1

この機能を使ってiPad上のポップオーバーを使ったほうがいいですか? AFAIK ActionSheetはボタン専用です。 – Eimantas

答えて

18

私はUIActionSheetがサイズ変更可能ではないと思われる、[pickerActionSheet addSubview:thePickerView];であなたのコード内の行をコメントしようとすると、あなたはActionSheetがボタンにperfecly合うことがわかります。

UIViewControllerUIPopoverControllerをお勧めします。このような何か:

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

ポップオーバーは.H(UIPopoverController *popover;)で宣言されたクラス変数です。

ところで、私はARCを使用しているので、コードにはリリースがありません。

8

私はそれが愚かな方法を使用して動作するように管理しました。

参考:
セットアップピッカービュー。

UIActionSheetの幅は何とか調整できないので、それに応じてピッカービューを調整する必要があります。高さを賢明に調整するには、"\ n"の額をactionSheetのタイトルで調整できます。

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を入れたときに、それは深刻なUIの遅延を得た理由を知らない(それがポップアップし、ほぼ1秒の遅れを取った)は私がすることはできません根本的な原因を見つける。だから上記の方法にフォールバックする必要があります。


ハッピーコーディング。

+0

ハッキーですが、これは普遍的なアプリで実装するのは少なくとも簡単です。ありがとう! –

関連する問題