【问题标题】:UILabel in UIAlertController or UITextField alike UILabelUIAlertController 或 UITextField 中的 UILabel 类似 UILabel
【发布时间】:2016-06-08 07:20:30
【问题描述】:

我需要给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 中的消息的行为不像标签。您是否基本上想要 UIAlert 中的自定义标签,以便您可以自定义它的属性(更改字体、颜色等)?请告诉我
  • @Chaitanya 我已经更新了我的问题。在那里你可以看到我尝试做的事情。

标签: ios objective-c uitextfield uilabel uialertcontroller


【解决方案1】:

我相信您想在 UIAlert 中添加自定义标签以获得良好的 UI 外观。

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

https://github.com/nealyoung/NYAlertViewController

https://github.com/sberrevoets/SDCAlertView

【讨论】:

    【解决方案2】:

    使用这个

    textField.placeholder = @"Text: ";
    

    而不是

    textField.text = @"Text: ";
    

    【讨论】:

    • 感谢您的回答,但这无济于事
    • 为什么要添加标签?
    • 我想要添加标签的一种情况是我的 textField 已经预先填充了数据(例如,当为用户提供编辑已经存在的信息的选项时),这意味着我不能使用占位符。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多