如何在代码创建的UIView中添加同样以代码方式生成的UITextView
问题复现
想要制作类似Instagram和Snapchat的文本接收视图,尝试在代码创建的UIView中同样用代码添加UITextView,现有代码组合运行后无法达到预期效果。
当前运行效果:
预期实现效果:
原始问题代码
import UIKit class TextViewViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let View1: UIView = { let viewView = UIView() viewView.translatesAutoresizingMaskIntoConstraints = false viewView.contentMode = .scaleAspectFit viewView.backgroundColor = .red viewView.clipsToBounds = true return viewView }() self.view.addSubview(View1) View1.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true View1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true View1.topAnchor.constraint(equalTo: view.topAnchor, constant: 250).isActive = true View1.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -250).isActive = true let textView = UITextView(frame: CGRect(x: 20.0, y: 90.0, width: 250.0, height: 100.0)) textView.contentInsetAdjustmentBehavior = .automatic textView.center = self.view.center textView.textAlignment = NSTextAlignment.justified textView.backgroundColor = UIColor.lightGray // Use RGB colour textView.backgroundColor = UIColor(red: 39/255, green: 53/255, blue: 182/255, alpha: 1) // Update UITextView font size and colour textView.font = UIFont.systemFont(ofSize: 20) textView.textColor = UIColor.white textView.font = UIFont(name: "Verdana", size: 17) // Make UITextView web links clickable textView.isSelectable = true textView.isEditable = false textView.dataDetectorTypes = UIDataDetectorTypes.link // Make UITextView corners rounded textView.layer.cornerRadius = 10 // Make UITextView Editable textView.isEditable = true View1.addSubview(textView) // // let textView: UITextView = { // let tv = UITextView(frame: CGRect(x: 20.0, y: 90.0, width: 100.0, height: 50.0)) // tv.contentInsetAdjustmentBehavior = .automatic // tv.center = self.view.center // tv.textAlignment = NSTextAlignment.justified // tv.textColor = UIColor.blue // tv.backgroundColor = UIColor.lightGray // return tv // }() // // View1.addSubview(textView) // textView.topAnchor.constraint(equalTo: View1.topAnchor, constant: 20).isActive = true // textView.bottomAnchor.constraint(equalTo: View1.bottomAnchor, constant: 20).isActive = true // textView.leadingAnchor.constraint(equalTo: View1.leadingAnchor, constant: 0).isActive = true // textView.trailingAnchor.constraint(equalTo: View1.trailingAnchor, constant: 0).isActive = true } }
解决方案
代码存在两个核心问题:
- 没有给UITextView设置
translatesAutoresizingMaskIntoConstraints = false,手动设置的frame、center和自动布局约束冲突,导致约束不生效 - 注释的约束代码中,底部约束常量错误,应该为负值才会向内缩进,否则textView底部会超出父视图边界
修正后完整代码
import UIKit class TextViewViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let containerView: UIView = { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false view.backgroundColor = .red view.clipsToBounds = true return view }() self.view.addSubview(containerView) NSLayoutConstraint.activate([ containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor), containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor), containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 250), containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -250) ]) let textView = UITextView() // 核心配置:禁用自动转frame为约束 textView.translatesAutoresizingMaskIntoConstraints = false textView.contentInsetAdjustmentBehavior = .automatic textView.textAlignment = .justified textView.backgroundColor = UIColor(red: 39/255, green: 53/255, blue: 182/255, alpha: 1) textView.font = UIFont(name: "Verdana", size: 17) textView.textColor = .white textView.isSelectable = true textView.isEditable = true textView.dataDetectorTypes = .link textView.layer.cornerRadius = 10 containerView.addSubview(textView) // 激活textView约束,上下左右相对于containerView缩进20 NSLayoutConstraint.activate([ textView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 20), textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -20), textView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 20), textView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -20) ]) } }
内容的提问来源于stack exchange,提问作者iosDevSan
相关产品推荐
相关产品推荐

