首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UILabel in UIAlertController或UITextField相像的UILabel

UILabel in UIAlertController或UITextField相像的UILabel
EN

Stack Overflow用户
提问于 2016-06-08 07:20:30
回答 3查看 3.8K关注 0票数 1

我需要添加一些标签到UIAlertController,经过研究发现,没有正常的方法去做它。但是,由于可以添加UITextField,我决定将UITextField的外观更改为类似的UILabel (没有边框和背景色)。但是,将其背景色更改为透明,将边框样式更改为none没有帮助。我如何做到这一点?

以下是代码:

代码语言:javascript
复制
- (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];
}

更新:

我得到的是:

下面是我想做的事:

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-08 07:51:38

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

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

https://github.com/nealyoung/NYAlertViewController

https://github.com/sberrevoets/SDCAlertView

票数 3
EN

Stack Overflow用户

发布于 2016-06-08 07:34:08

使用这个

代码语言:javascript
复制
textField.placeholder = @"Text: ";

而不是

代码语言:javascript
复制
textField.text = @"Text: ";
票数 0
EN

Stack Overflow用户

发布于 2020-08-26 07:22:28

不需要第三方库来实现这一点。只需自定义UIAlertController

代码语言:javascript
复制
            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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37695744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档