LJulz
LJulz

Reputation: 123

XIB isn't loaded properly in UIScrollView

I have a scrollview with paging. In this scroll view i load dynamically xibs. But the height of the xib should be the same as the scroll view. My problem is that the height of the xib is not loaded properly:

If I load a the xib file (which is almost fullscreen except on each border a constraint of 10) it isn't loaded properly because the height is too big.

So I have to make the constraint of bottom so that it fits into my scrollview:

enter image description here

Anyone got a solution?

Upvotes: 0

Views: 214

Answers (1)

devshok
devshok

Reputation: 836

Try to experiment with the code below. This general approach works for my project. Your problem is ambiguous definition for height property. Pay attention to find unnecessary constraints in your .xib.

@IBOutlet weak var yourCustomScrollView: UIScrollView!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Firstly, define scrollview's position and size
    yourCustomScrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

    // Load .xib with custom class from main bundle.        
    guard let xib = Bundle.main.loadNibNamed("YourXibName", owner: self, options: nil)?.first as? YourCustomXibClass else {
        fatalError("YourXibName is not found. 👎🏻")
    }
    self.yourCustomScrollView.addSubView(xib)

    // Define .xib's position and size
    xib.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
    yourCustomScrollView.contentSize = CGSize(width: self.view.frame.width, height: xib.frame.height)
}

Upvotes: 1

Related Questions