6

誰かがこのコードを別の質問に投稿して、UIAlertSheet に UIDatePicker を配置しました。

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                                  delegate:self
                                         cancelButtonTitle:@"Cancel"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];

// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[menu addSubview:pickerView];
[menu showInView:self.view];        
[menu setBounds:CGRectMake(0,0,320, 500)];

CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;

[pickerView release];
[menu release];

これを次のように変更しました。

    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil
                                                      delegate:nil
                                             cancelButtonTitle:@"Done"
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];

    // Add the picker
    UIDatePicker *pickerView = [[UIDatePicker alloc] init];
    pickerView.datePickerMode = UIDatePickerModeCountDownTimer;
    [menu addSubview:pickerView];
    [menu showInView:self.navigationController.tabBarController.view];        
    [menu setBounds:CGRectMake(0,0,320, 516)];
    [pickerView setFrame:CGRectMake(0, 85, 320, 216)];

これにより、アラート シートが正しく配置されます (nil デリゲートは無視してください。これは単に表示用です)。問題はここにあります:

代替テキスト
(出典:hosting-wizards.com

ご覧のとおり、ミニッツ スクローラーの上にレイヤリングの問題があります。これがデバイスで発生するかどうかは確認していません。なぜこれが起こっているのかについてのアイデアはありますか?

別の質問: UIActionSheet の setBounds セレクターで、正しく表示するために Y サイズが非常に高い値に設定されているのはなぜですか? Y サイズを 480 に設定すると、全画面表示になりますよね?この値をゼロに設定すると、ほとんど見えなくなります。

補足: 上に貼り付けたコードは、UIDatePicker を画像と同じ場所に配置しませんが、UIDatePicker がどこに配置されていてもレイヤー化の問題が発生します。重ね着の問題は上だけで、下ではありません。

4

5 に答える 5

8

UIDatePicker を新しいビューに配置し、下から上にスライドする動作を自分で実装するだけで、表示の問題を回避し、はるかに見栄えの良い結果を得ることができます。それほど難しくありません。

  1. 日付ピッカーと [完了] ボタンのあるツールバーを含む UIView を作成します。(コード内または Interface Builder の使用は問題ではありません。)
  2. ポップアップ ビューをメイン ビューのサブビューとして追加します。
  3. ポップアップ ビューのフレームが画面の下部からはみ出すように設定します。frame.origin.y = CGRectGetMaxY(mainView.bounds)
  4. 電話[UIView beginAnimations:nil context:nil]
  5. フレームをあるべき場所に設定します。frame.origin.y -= CGRectGetHeight(popupView.bounds)
  6. 電話[UIView commitAnimations]

(フレーム設定は疑似コードであることに注意してください。)

それでおしまい。ビューを閉じる必要がある場合は、逆に行います。苦労する価値はあると思います。UIActionSheet に日付ピッカーを貼り付けることは、信じられないほど厄介に見えます。

于 2010-01-08T09:48:32.207 に答える
3

上記の benzado の手順を使用すると、ActionSheet の代わりに UIView を使用する場合、コードは大まかに次のようになります。

int height = 255;

//create new view
UIView * newView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 320, height)];
newView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];

//add toolbar
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 40)];
toolbar.barStyle = UIBarStyleBlack;

//add button
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:nil];
toolbar.items = [NSArray arrayWithObjects:infoButtonItem, nil];

//add date picker
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
datePicker.frame = CGRectMake(0, 40, 320, 250);
[datePicker addTarget:self action:nil/*@selector(changeDateInLabel:)*/ forControlEvents:UIControlEventValueChanged];
[newView addSubview:datePicker];

//add popup view
[newView addSubview:toolbar];
[self.view addSubview:newView];

//animate it onto the screen
CGRect temp = newView.frame;
temp.origin.y = CGRectGetMaxY(self.view.bounds);
newView.frame = temp;
[UIView beginAnimations:nil context:nil];
temp.origin.y -= height;
newView.frame = temp;
[UIView commitAnimations];

同意します。ピッカーではなくボタンを表示するように設計された ActionSheets を使用するよりもはるかに見栄えがします。「完了」ボタンと、ユーザーがピッカーを変更したときに独自のアクション ハンドラーを追加する必要がありますが、これで作業を開始できます。

于 2011-09-12T21:19:54.933 に答える
1

これを試して

[menu sendSubviewToBack: pickerView];

また、スクリーンショットに表示されているように、ピッカーを10ピクセル下に移動します。これは、下部に配置されていない可能性があります。

[pickerView setFrame:CGRectMake(0, 100, 320, 216)];
于 2011-01-06T05:04:30.847 に答える
1

ピッカーを UIActionSheet に配置する必要は本当にありますか? UIResponder クラスの inputView プロパティを使用しないのはなぜですか? コントロール (UITextField など) がファーストレスポンダーになると、UIDatePicker (またはその他のビュー) が自動的に表示されます。ソース コードで inputView を使用する例は、ここにあります: Working with pickers 。

于 2010-10-01T09:42:49.187 に答える
0

次のスレッドが別のアプローチに役立つかもしれません: http://discussions.apple.com/message.jspa?messageID=7923095

于 2010-01-06T21:49:14.563 に答える