我有一个UITextView,为什么我试图用autolayout约束它,这真的很奇怪。背景将被放置正确,但其中的文本不合适。我尝试创建一个UIView,然后将其添加到UIView,但问题仍然存在。
这是UITextView和UIView声明
let noteTextView: UITextView =
{
let tv = UITextView()
tv.textColor = UIColor.white
tv.backgroundColor = UIColor.clear
tv.font = UIFont.systemFont(ofSize: 16)
tv.isEditable = false
tv.isUserInteractionEnabled = true
return tv
}()
let backgroundView: UIView =
{
let view = UIView()
view.backgroundColor = UIColor.rgb(red: 156, green: 44, blue: 252)
return view
}()
这是约束(使用名为锚的扩展名)
view.addSubview(backgroundView)
backgroundView.addSubview(noteTextView)
backgroundView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 65, paddingLeft: 0, paddingRight: 0, paddingBottom: 0, width: 0, height: 80)
noteTextView.anchor(top: backgroundView.topAnchor, left: backgroundView.leftAnchor, bottom: backgroundView.bottomAnchor, right: backgroundView.rightAnchor, paddingTop: 2, paddingLeft: 5, paddingRight: 5, paddingBottom: 2, width: 0, height: 0)
以下是展示问题的视频:https://streamable.com/y8v70
注意:当我刚刚将UITextView添加到视图中时(没有UIVIew),发生了同样的事情。
答案 0 :(得分:1)
在automaticallyAdjustsScrollViewInsets
viewDidLoad
self.automaticallyAdjustsScrollViewInsets = false
另请尝试:
textView.textContainerInset = UIEdgeInsetsZero;
textView.textContainer.lineFragmentPadding = 0;
愿这有帮助。
答案 1 :(得分:0)
不要给UIView或UITextView提供静态高度。
尝试此功能:
func textviewHeight() {
let contentSize = self.TextView.sizeThatFits(self.TextView.bounds.size)
var frame = self.TextView.frame
frame.size.height = contentSize.height
self.TextView.frame = frame
self.aspectRatioTextView = NSLayoutConstraint(item: self.TextView, attribute: .Height, relatedBy: .Equal, toItem: self.TextView, attribute: .Width, multiplier: TextView.bounds.height/TextView.bounds.width, constant: 1)
self.TextView.addConstraint(self.aspectRatioTextView!)
self.mainViewHeightConstraint.constant = self.mainViewHeightConstraint.constant + self.TextView.frame.height
}
可能会帮助你!!!!!