ios - UIAlertViewConroller 中对 UITextView 的约束

标签 ios swift uitextview uialertviewcontroller

我在 UIAlertViewController 中有一个 UITextView。我需要以编程方式为 UITextView 设置前导和尾随约束。

截图

enter image description here

这是我的代码

func popUpController()
{
    let alert = UIAlertController(title: "Additional Notes", message: "Write your additional notes here.", preferredStyle: .alert)

    let textView = UITextView()
    textView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    let color = UIColor(red: 224/255, green: 224/255, blue: 224/255, alpha: 1.0).cgColor

    textView.layer.borderColor = color
    textView.layer.borderWidth = 1.0
    textView.layer.cornerRadius = 5.0

    let controller = MyActivitiesViewController()

    textView.frame = controller.view.frame
    controller.view.addSubview(textView)

    alert.setValue(controller, forKey: "contentViewController")

    let height: NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 180)
    alert.view.addConstraint(height)

    let saveAction = UIAlertAction(title: "OK", style: .default, handler: nil)
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    alert.addAction(saveAction)
    alert.addAction(cancelAction)


    self.present(alert, animated: true, completion: {

        textView.becomeFirstResponder()
    })

}

最佳答案

请注意,这只是一种解决方法。

如果您想为警报 Controller 提供更多高度,请将更多新行添加到title

如果您想修改此内容,请根据您的要求调整约束

 func showAlertController()
    {

        let alertController = UIAlertController(title: "Your title \n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.alert)
        let customView = UITextView()
        customView.translatesAutoresizingMaskIntoConstraints = false
        alertController.view.autoresizesSubviews = true
        let somethingAction = UIAlertAction(title: "Some Action", style: UIAlertActionStyle.default, handler: {(alert: UIAlertAction!) in

            print(customView.text)

        })

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

        alertController.addAction(somethingAction)
        alertController.addAction(cancelAction)


        alertController.view.addSubview(customView)

        customView.topAnchor.constraint(equalTo: alertController.view.topAnchor, constant: 50).isActive = true

        customView.rightAnchor.constraint(equalTo: alertController.view.rightAnchor, constant: -10).isActive = true
        customView.leftAnchor.constraint(equalTo: alertController.view.leftAnchor, constant: 10).isActive = true
        customView.bottomAnchor.constraint(equalTo: alertController.view.bottomAnchor, constant: -60).isActive = true
        customView.backgroundColor = UIColor.red
        self.present(alertController, animated: true) {

        }

    }

enter image description here

关于ios - UIAlertViewConroller 中对 UITextView 的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51497539/

相关文章:

ios - 如何在屏幕锁定/后台模式下播放音频

ios - 如何制作类似 Instagram 的标签页?

ios - JSONModel:填充泛型类型的 NSArray

json - 在 Swift 2.0 中从 JSON 对象获取值

swift - 具有子类类型的类方法调用闭包的基类

uitextview - 如何在 IB 中的 UITextView 中输入新行

ios - UITextView 点击一个单词的中间并将光标放在那里

swift - swift 中带反斜杠的正则表达式

ios - UITabBarController 不显示其他选项卡的标题

swift - 将 textfield.text 与 firebase 字符串 swift 进行比较