簡體   English   中英

UIAlertController 中的 UILabel 或 UITextField 類似 UILabel

[英]UILabel in UIAlertController or UITextField alike UILabel

我需要向UIAlertController添加一些標簽,經過研究后發現沒有正常的方法可以做到這一點。 但是由於可以添加UITextField ,我決定將UITextField的外觀更改為與UILabel相似(沒有邊框和背景顏色)。 但是將其背景顏色更改為 clear 並將邊框樣式更改為 none 並沒有幫助。 我怎樣才能做到這一點?
這是代碼:

- (void)test
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Test Title"
                                                                    message:@"Test Message"
                                                             preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                                   //Do Some action here

                                               }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                       [alert dismissViewControllerAnimated:YES completion:nil];
                                                   }];

    [alert addAction:ok];
    [alert addAction:cancel];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.text = @"Text: ";
        textField.backgroundColor = [UIColor blueColor];
        textField.borderStyle = UITextBorderStyleNone;
        textField.backgroundColor = [UIColor clearColor];
        [textField setUserInteractionEnabled:NO];
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    }];

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

更新:
這是我得到的:
我有什么
這就是我想要做的:
我想做什么

我相信您想在 UIAlert 中添加自定義標簽以獲得良好的 UI 外觀。

執行此操作的最佳方法是編寫自定義 UIView 使其感覺和行為類似於 UIAlertView 或使用以下來自 github 的庫之一。

https://github.com/nealyoung/NYAlertViewController

https://github.com/sberrevoets/SDCAlertView

用這個

textField.placeholder = @"Text: ";

而不是

textField.text = @"Text: ";

不需要第三方庫來實現這一點。 只需自定義UIAlertController

            let alertController = UIAlertController(title: "Add Table", message: "", preferredStyle: UIAlertController.Style.alert)
            
            alertController.addTextField { (textField) -> Void in
                textField.text = "No of Columns :"
                textField.keyboardType = .numberPad
                textField.isUserInteractionEnabled = false
            }
            
            alertController.addTextField { (textField) -> Void in
                textField.placeholder = "No of Columns"
                textField.keyboardType = .numberPad
                textField.text = "2"
                textField.isUserInteractionEnabled = false
            }
            
            alertController.addTextField { (textField) -> Void in
                textField.text = "No of Rows :"
                textField.keyboardType = .numberPad
                textField.isUserInteractionEnabled = false
            }
            
            alertController.addTextField { (textField) -> Void in
                textField.placeholder = "No of Rows"
                textField.keyboardType = .numberPad
                textField.text = "2"
            }
            let saveAction = UIAlertAction(title: "Create", style: UIAlertAction.Style.default, handler: { alert -> Void in
                let firstTextField = alertController.textFields![0] as UITextField
                let secondTextField = alertController.textFields![1] as UITextField
            })
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.destructive, handler: {
                (action : UIAlertAction!) -> Void in })
            
            alertController.addAction(cancelAction)
            alertController.addAction(saveAction)

            if let textFields = alertController.textFields {
                if textFields.count > 0{
                    textFields[0].superview!.superview!.subviews[0].removeFromSuperview()
                    textFields[0].superview!.backgroundColor = UIColor.clear
                }
                
                if textFields.count > 2{
                    textFields[2].superview!.superview!.subviews[0].removeFromSuperview()
                    textFields[2].superview!.backgroundColor = UIColor.clear
                }
            }
            
            self.present(alertController, animated: true, completion: nil)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM