0

I am beginner. Thank you for help. I want to add a custom view to scrollview. I have to set label text but can't do because it's nil. I tried this way.

var y = 50;
self.scrollView.contentSize = CGSize(width: 350, height: 200);
for i in 0...10
{
    let view = MyView2();
    view.translatesAutoresizingMaskIntoConstraints = false;
    view.frame = CGRect(x: 100, y: y, width: 300, height: 100);
    y = y + 150;
    view.lblName.text = "Test : \(i)";
    self.scrollView.contentSize = CGSize(width: 350, height: y);
    self.scrollView.addSubview(view); 
}

This is my customView. I added xib file and I am sure I choose xib file custom class.

class MyView2: UIView {
    @IBOutlet weak var lblName: UILabel!
    @IBOutlet weak var myButton: UIButton! 
}

1 Answer 1

2

Initialise the view like that-

 let view = UINib(nibName: "nib file name", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.