Search code examples
iosswiftuiscrollviewxib

Swift scrollview adding from XIB not scrollable at all


I'm trying to import from Xib full screen ScrollView into my ViewController.

Following that guide i've made many working examples of it, but when importing it from Xib, ScrollView not responding on scroll (not even bouncing)

My Xib View class:

class TestScroll: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func awakeFromNib() {
    super.awakeFromNib()

    self.translatesAutoresizingMaskIntoConstraints = false
}

public static func getViewFromNib() -> TestScroll {
    return UINib(nibName: "TestScroll", bundle: .main).instantiate(withOwner: nil, options: nil).first as! TestScroll
    }
}

And this is how i adding it in ViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    let testScroll = TestScroll.getViewFromNib()
    self.view.addSubview(testScroll)
}

Please help, i've checked many guides already, but not found working example with Xib.


Solution

  • You need to set a frame / constraints

    let testScroll = TestScroll.getViewFromNib()
    testScroll.frame = self.view.bounds
    self.view.addSubview(testScroll)
    

    leave this

    self.translatesAutoresizingMaskIntoConstraints = false
    

    only if you'll set constraints