【问题标题】:Constraints to UITextView inside UIAlertViewConrollerUIAlertViewConroller 中对 UITextView 的约束
【发布时间】:2019-01-01 00:17:37
【问题描述】:

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

截图

这是我的代码

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()
    })

}

【问题讨论】:

  • 根据 Apple 文档:**Important** The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified. 你最好展示自己的控制器,而不是尝试做你明确应该做的事情。

标签: ios swift uitextview uialertviewcontroller


【解决方案1】:

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

如果您想为警报控制器提供更多高度,请在 title 中添加更多新行。。 p>

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

 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) {

        }

    }

【讨论】:

  • @MinnuKaAnae 更新答案请检查
  • @Anurai 试过了,如何降低 customView 的高度
  • @MinnuKaAnae。您可以减少标题中换行符的数量,或者您应该调整顶部或底部锚点偏移值。
猜你喜欢
  • 1970-01-01
  • 2016-11-18
  • 2014-06-07
  • 2014-06-28
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
相关资源
最近更新 更多