2017-04-24 16 views
2

私はアプリを使っていますが、かなり古いアプリです。クライアントはあまりにも多くの時間を費やしたくないので、私はこの問題のための簡単で迅速な修正を探しています。iOS AlertController(マルチラインUITextField)

ユーザーはプロファイルのサマリを書く必要があるので、大きなスペースが必要です。アプリケーション全体がAlertControllerのポップアップで構築されていますので、1行のUITextFieldをより大きな複数行に置き換えたいUITextField。

私はそれを達成するために何ができますか? は、私は現在、次のコードを追加しました:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"Enter your summary" preferredStyle:UIAlertControllerStyleAlert]; 
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 
     textField.placeholder = @"Summary"; 
     textField.autocapitalizationType = UITextAutocapitalizationTypeSentences; 
     textField.text = _summaryLabel.text; 

     NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant: 110.0]; 
     [textField addConstraint:constraint]; 
     [textField layoutIfNeeded]; 
    }]; 

UITextFieldのは、実際に大きいことを意味し、それは複数行ではありませんし、それはまた、垂直方向にテキストを中心としています。これについて私は何ができますか?

+4

'UITextField'は単一行のみです。複数行が必要な場合は、 'UITextView'が必要です。これは' UIAlertController'でサポートされていません。 – rmaddy

+1

'UIAlertController'と同様に動作するxibを使用してカスタムビューを作成するのはあまり複雑ではありません。 – shim

答えて

2

@rmaddyが言っているように、UITextFieldは1行のみの場合、UITextViewを使用できます。

あなたは、このことにより、それを達成することができます

How to use UITextView in UIAlertController

UIAlertController *alert = [UIAlertController 
          alertControllerWithTitle:@"Enter your summary" 
          message:@"\n\n\n\n\n\n\n\n" 
          preferredStyle:UIAlertControllerStyleAlert]; 
alert.view.autoresizesSubviews = YES; 

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero]; 
textView.translatesAutoresizingMaskIntoConstraints = NO; 

NSLayoutConstraint *leadConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:-8.0]; 
NSLayoutConstraint *trailConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:8.0]; 

NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-64.0]; 
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:alert.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:textView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:64.0]; 
[alert.view addSubview:textView]; 
[NSLayoutConstraint activateConstraints:@[leadConstraint, trailConstraint, topConstraint, bottomConstraint]]; 

[alert addAction: [UIAlertAction actionWithTitle:@"Done"style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) { 
    NSLog(@"%@", textView.text); 
}]]; 

[self presentViewController:alert animated:YES completion:nil]; 

UIAlertController

UIAlertViewを使用します。

How to Insert the UITextView into UIAlertview in iOS

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                message:@"Enter your summary" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"Done", nil]; 

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero]; 
[alert setValue:textView forKey:@"accessoryView"]; 
[alert show]; 

UIAlertView

またはカスタムios-custom-alertview

+1

2番目のオプション(UIAlertView 1)はプライベートAPIを使用しませんか? 私はUIAlertControllerを伸ばすが、何らかの形で正しいtextViewを表示しないので、最初の解決策を得るのに成功していないようだ。 編集:私はそれを解決しました、私はメッセージに改行が見られませんでした。あなたのソリューションをありがとう:) – jbehrens94

0

のような独自のアラートは、あなたはAlertController内のViewControllerを埋め込むことができます。

-(void)showAlertWithTitle:(NSString *)title andBody:(NSString *)body andTextToEdit:(NSString*)text{ 

    UIViewController *controller = [[UIViewController alloc]init]; 

    // Some size 
    CGRect rect = CGRectMake(0, 0, 272, 250); 
    [controller setPreferredContentSize:rect.size]; 

    // The text view to be used 
    UITextView *textView = [[UITextView alloc]initWithFrame:rect]; 

    [textView setText:text]; // Original text is there to edit 
    [controller.view addSubview:textView]; // Add the textField to the controller 

    // I needed these when adding a UITableView, not sure if needed for UITextView 
    [controller.view bringSubviewToFront:textView]; 
    [controller.view setUserInteractionEnabled:YES]; 

    alertWithText = [UIAlertController alertControllerWithTitle:title message:body preferredStyle:UIAlertControllerStyleAlert]; 
    [alertWithText setValue:controller forKey:@"contentViewController"]; 

    // create the actions handled by each button 
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

     NSLog(@"The Edited Text : %@", textView.text); 
    }]; 

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { 

    }]; 


     // add the actions to our alert 
    [alertWithText addAction:action1]; 
    [alertWithText addAction:action2]; 

    [self presentViewController:alertWithText animated:YES completion:^{ 

    }]; 
} 
関連する問題