2016-08-26 34 views
0

アラートコントローラに日付パイーカーを追加しようとしましたが、サイズに問題があります。私は日付pikerの高さを取得する方法や日付pikerの高さを持つinit UIAlertControllerを知ることはできません。 だから、私がやった:UIAlertControllerにUIDatePickerを追加する方法iOS9

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet]; 

UIView *viewDatePicker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]; 
[viewDatePicker setBackgroundColor:[UIColor clearColor]]; 

CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200); 
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame]; 
[picker setDatePickerMode:UIDatePickerModeTime]; 

[viewDatePicker addSubview:picker]; 

[alertController.view addSubview:viewDatePicker]; 

NSDateFormatter *formate=[[NSDateFormatter alloc]init]; 
[formate setDateFormat:@"hh:mm a "]; 
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 

                  sinceDateString = [formate stringFromDate:picker.date]; 
                  [sinceDateButton setTitle:sinceDateString forState:UIControlStateNormal]; 

                 }]; 

[alertController addAction:defaultAction]; 
[self presentViewController:alertController animated:YES completion:nil]; 

私はこの解決策を見つける同じ質問UIAlertController の1 * alertController = [UIAlertController alertControllerWithTitle:nilにメッセージ: "@ \ N \ N \ N \ N \ N \ N \ n \ n \ n \ n " preferredStyle:UIAlertControllerStyleActionSheet];

しかし、私は灰色の線が見えるので間違った解決策だと分かります。 UIAlertController

+0

これは現在選択されている日付の選択行です。 –

+0

ありがとうございます。しかし、 "\ n \ n \ n"については、UIAlertControllerの高さを追加する唯一の解決策がありますか? –

+0

\ nは新しい行を作成することを意味します –

答えて

2

あなたがこのexample

\ nは改行文字であることがわかり、あなたのコンセプトのためUIDatePickerを選びましたしていない理由、それは次の行の

を開始する位置を移動し、あなたの2番目の質問はの灰色の行を示しています、それはあなたのdatepickerの選択領域を意味します。あなたのコード内

あなたは\ N \ N \ N \ N \ N \ N \ N \ N回10で、2 \ N \削除を使用nは、その後完璧示しています。

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet]; 

UIView *viewDatePicker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,200)]; 
[viewDatePicker setBackgroundColor:[UIColor clearColor]]; 

CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200); 
UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:pickerFrame]; 
[picker setDatePickerMode:UIDatePickerModeTime]; 

[viewDatePicker addSubview:picker]; 

[alertController.view addSubview:viewDatePicker]; 

NSDateFormatter *formate=[[NSDateFormatter alloc]init]; 
[formate setDateFormat:@"hh:mm a"]; 
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 

                  //                sinceDateString = [formate stringFromDate:picker.date]; 
                  //                [sinceDateButton setTitle:sinceDateString forState:UIControlStateNormal]; 

                 }]; 

[alertController addAction:defaultAction]; 
[self presentViewController:alertController animated:YES completion:nil]; 

と私はアクションシートにUIDatePickerを追加

enter image description here

+0

アップデートの回答を確認してください。ここで私の提案では 'UIDatePicker'を使用しています –

+0

ありがとう、 –

1

のような出力を得ましたが、すべてに沿って、アップルによって落胆ました。 iOS 7以降、Appleはインラインデートピッカーの使用を導入しました(Calendarアプリでどのように行われているか参照してください)。

UIAlertControllerを使用して回避策をハックした場合、将来のiOSリリースで再び破損する可能性があります。

関連する問題