Background info I am creating a consent/question form application. This form will have several different questions with text in different lengths. One line up to 5 lines depending on the device.
This is what i have done so far I have created a custom UIView class with an XIB file where i have added one Label anchored to the top (Lines set to 0 = multiline for that label). This label will a short or a long text added to it.
Underneath the label i have a yes/no buttons as radio group lookalike and under there again I will have a UIText field hidden by default, that will only be visible on some of the question and only if the user selects "Yes"
The question will be added to a vertical UIStackview who sits on top of a UIScrollView
myQuestionView = MyQuestionView(preDefinedAnswer: false, preDefinedAnswerText: nil)
myQuestionView?.questionLabel.text = "Lorem ipsum dolor sit amet, consectetur adipiscing ?"
myQuestionView?.shouldShowAnswerTextfield = true
myQuestionView?.heightAnchor.constraint(equalToConstant: 120).isActive = true
myStackView.addArrangedSubview(myQuestionView!)
The problem As you can see in the code above, I am setting the height constraint to 120, and that looks ok if the question is only one line and if the user DONT select yes (then the view should grow and show the answertextfield). Normally i would maybe created a height constraint outlet and tried to calculate the height within the UIView class, but I am not able to to that.
The question How can i dynamically set the height of my custom UIView class based on its content AND get the stackview to grow accordingly? I want the height to be correct when i instantiate it and the grow if the user clicks yes (to show the UITextfield).
Thanks for any help.
The UIView class
import UIKit
class MyQuestionView: UIView {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var answerTextfield: UITextField!
@IBOutlet weak var jaButton: UIButton!
@IBOutlet weak var neiButton: UIButton!
@IBInspectable var height: CGFloat = 300.0
@IBOutlet weak var viewHeight: NSLayoutConstraint!
var shouldShowAnswerTextfield : Bool?
var answerText : String?
var didAnswerYes : Bool?
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
init(preDefinedAnswer:Bool?, preDefinedAnswerText:String?)
{
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 200) )
didAnswerYes = preDefinedAnswer
answerText = preDefinedAnswerText
commonInit()
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
setup()
print("init")
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
setup()
print("Init?")
}
func commonInit(){
let viewFromXib = Bundle.main.loadNibNamed("MyQuestionView", owner: self, options: nil)![0] as! UIView
viewFromXib.frame = self.bounds
addSubview(viewFromXib)
}
func setup()
{
answerTextfield.isHidden = true
if(didAnswerYes != nil)
{
print("Setup \(didAnswerYes)")
setAnswer(didAnswerYes!)
if(answerText != nil)
{
answerTextfield.text = answerText
if(didAnswerYes!)
{
answerTextfield.isHidden = false
}
}
}
//sett constraints
//self.heightAnchor.constraint(equalToConstant: 320).isActive = true
//self.sizeToFit()
//self.layoutIfNeeded()
}