UILabel在UIAlertController或UITextField中都是UILabel

时间:2016-06-08 07:20:30

标签: ios objective-c uitextfield uilabel uialertcontroller

我需要为UIAlertController添加一些标签,经过研究后发现没有正常的方法可以做到这一点。但由于可以添加UITextField,我决定将UITextField的外观更改为UILabel(没有边框和背景颜色)。但是将其背景颜色更改为清晰并将边框样式更改为无,这无济于事。我怎样才能做到这一点?
这是代码:

- (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];
}

更新
这是我得到的:
What I have
以下是我要做的事情:
What I trying to do

3 个答案:

答案 0 :(得分:3)

我相信你想在UIAlert中添加一个自定义标签以获得良好的UI Look。

执行此操作的最佳方法是编写自定义UIView,使其感觉和行为类似于UIAlertView,或者使用github中的以下库之一。

https://github.com/nealyoung/NYAlertViewController

https://github.com/sberrevoets/SDCAlertView

答案 1 :(得分:0)

使用此

textField.placeholder = @"Text: ";

而不是

textField.text = @"Text: ";

答案 2 :(得分:0)

不需要第三方库来实现此目的。只需自定义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)

enter image description here