我有一个UITextView
内幕UIAlertViewController
。我需要以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()
})
}