Reputation: 751
I have a UITextView that is acting really weird why I try to constrain it with autolayout. The background will be placed right, but the text within is out of place. I tried to create an UIView, and then add it to the UIView instead, but the problem still persists.
Here is the UITextView and UIView declarations
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
}()
And here is the constraints (using an extension called anchor)
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)
Here is a video to demonstrate the problem: https://streamable.com/y8v70
Note: When I just added the UITextView to the view (without the UIVIew) the exact same thing happened.
Upvotes: 0
Views: 89
Reputation: 4995
Disable automaticallyAdjustsScrollViewInsets
in viewDidLoad
self.automaticallyAdjustsScrollViewInsets = false
Try also:
textView.textContainerInset = UIEdgeInsetsZero;
textView.textContainer.lineFragmentPadding = 0;
May this help.
Upvotes: 1
Reputation: 652
Do not give a static height to UIView or to UITextView.
Try this Function:
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
}
May be it helps you !!!!!
Upvotes: 0