iOS UIAlertview与uitextview可编辑

问题描述 投票:0回答:2

我正在寻找一种在UIAlertView中放置UITextView的方法。

我知道如何放一个简单的文本域:

alert.alertViewStyle = UIAlertViewStylePlainTextInput;

但这不是我想要的。我想要更大的文本输入,以便用户可以在新闻之后编写评论。

这可能吗 ?

ios uitextview uialertview
2个回答
1
投票

不幸的是,你所追求的是UIAlertView无法实现的。

Apple不允许开发人员修改UIAlertView的视图层次结构或将其子类化。看看Apple Documentation for UIAlertView。在标记为Subclassing note的部分下,您将找到

UIAlertView类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

不幸但是因为UIAlertView仍然有addSubview:方法,所以它与Apple实际告诉我们的相反,但原因仍然是因为UIAlertViewUIView的子类,它有这种方法。所以Apple所做的是他们已经覆盖了这个方法,所以它绝对没有任何意义,所以当你调用[myAlertView addSubview:myView];时什么都不做,而且没有任何视图会添加到UIAlertView

因此,要获得您所需的行为,您将需要实现自定义AlertView(查看Google搜索Custom UIAlertView)。

幸运的是,在iOS 8中,Apple引入了一个名为UIAlertController的新类,它允许您获得您所追求的行为,并且他们已经弃用了UIAlertView类。


0
投票

将自定义视图添加到警报视图中。

根据要求将preferredStyle更改为.alert和.actionsheet。

func showPopUpWithTextView() {
        let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: .actionSheet)

        let margin:CGFloat = 8.0
        let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 100.0)
        let textView = UITextView(frame: rect)
        textView.backgroundColor = .clear

        alertController.view.addSubview(textView)

        let submitAction = UIAlertAction(title: "Something", style: .default, handler: {(alert: UIAlertAction!) in print("Submit")
            print(textView.text)
        })

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(alert: UIAlertAction!) in print("cancel")})

        alertController.addAction(submitAction)
        alertController.addAction(cancelAction)

        self.present(alertController, animated: true, completion:{})
    }

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.