2017-04-24 34 views
2

我正在開發一個應用程序,但它是一個相當老的應用程序。客戶不想花費太多時間,所以我正在尋找一個簡單而快速的解決方案來解決這個問題。帶多行UITextField的iOS AlertController

用戶必須爲他們的個人資料寫一個摘要,所以他們需要一個很大的空間來寫入。整個應用程序是用AlertController彈出窗口構建的,所以我想用一個更大的多行代替單行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只適用於單行,你可以使用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

第二個選項(UIAlertView之一)不會使用私有API嗎? 我也似乎沒有成功獲得第一個解決方案的工作,因爲它拉伸UIAlertController,但不知何故不會顯示我正確的textView。 編輯:我解決了它,我沒有看到消息中的新行。感謝您的解決方案:) – jbehrens94

0

提醒您可以嵌入AlertController內的視圖控制器。

-(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:^{ 

    }]; 
}