Reputation: 839
I have a UITextView
in a UIView
that I would like the user to be able to show and hide by tapping on it. The UITextView
and a constraint of 10 on each side. Currently when a user taps on the view the text does disappear, however you can still see where the UITextView
is because there is about one line of space where the UITextView
was.
Here is the code I use to adjust the visibility of the UITextView
:
if(collapseArray[indexPath.row]){
cell.noteText?.text = nil
}else{
cell.noteText?.text = notesArray[indexPath.row]
}
I have tried setting the .isHidden
attribute but I get the same results. I was able to get the desired result when I used a UILabel
however I would like the text to be editable.
Here is what I am talking about
As you can see there is still a large about of space below the word test, which is a UILabel
that has constraints of 10 all the way around.
Here is the code I used for the given answer below:
class NoteTableViewCell: UITableViewCell, UITextViewDelegate {
@IBOutlet weak var noteTitle: UILabel!
@IBOutlet weak var noteText: UITextView!
@IBOutlet weak var cardView: UIView!
var textViewHeightConstraint: NSLayoutConstraint!
override class func awakeFromNib() {
textViewHeightConstraint = noteText.heightAnchor.constraint(equalToConstant: 0)
textViewHeightConstraint.priority = .defaultLow
textViewHeightConstraint.isActive = true
}
}
however this gives me the error:
Instance member 'noteText' cannot be used on type 'NoteTableViewCell'
Upvotes: 0
Views: 261
Reputation: 20379
In your cell, hold a reference to height constraint of textView var textViewHeightConstraint: NSLayoutConstraint!
, probably in awakefromNib, be aware that constant is 0 and priority is low
textViewHeightConstraint = noteText.heightAnchor.constraint(equalToConstant: 0)
textViewHeightConstraint.priority = .defaultLow
textViewHeightConstraint.isActive = true
In your logic, toggle the priority of constraint
if(collapseArray[indexPath.row]){
cell.textViewHeightConstraint.priority = .required //this should set textView height to 0
}else{
cell.textViewHeightConstraint.priority = .defaultLow //this should set it back
}
cell.textViewHeightConstraint.isActive = true
cell.layoutIfNeeded()
EDIT:
As OP is facing issue with initializing constraint property in awakeFromNib
, I am updating the answer below
class NoteTableViewCell: UITableViewCell, UITextViewDelegate {
@IBOutlet weak var noteTitle: UILabel!
@IBOutlet weak var noteText: UITextView!
@IBOutlet weak var cardView: UIView!
var textViewHeightConstraint: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
textViewHeightConstraint = noteText.heightAnchor.constraint(equalToConstant: 0)
textViewHeightConstraint.priority = .defaultLow
textViewHeightConstraint.isActive = true
}
}
You are supposed to use override func awakeFromNib()
not override class func awakeFromNib()
also you are supposed to call super.awakeFromNib()
before you go ahead and initialize your constraint, super.awakeFromNib()
will ensure your IBOutlets
are initialized by the time your first statement to initialize constraint executes.
Upvotes: 1