Search code examples
swiftuiscrollviewtvos

tvOS: UIScrollView doesn't scroll


I am trying to reproduce natively the TVML template that provides a grid of clickable images that extends beyond the screen's bounds. I am using a scroll view for this attempt, but I am unable to select elements that are added to the scroll view, but outside its visible area.

The sketch code using buttons for simplicity is as follows:

let dim = 50

for i in 0..<10 {
    for j in 0..<10 {
        let frame = CGRect(x: i * (dim + 10), y: j * (dim + 10), width: dim, height: dim)
        let button = UIButton(type: .System)
        button.frame = frame
        myScrollView.panGestureRecognizer.allowedTouchTypes = [UITouchType.Indirect.rawValue]

        myScrollView.addSubview(button)
    }
}

The scroll view is sized such that only half of these buttons are visible. Why is the scroll view not scrolling to the buttons outside this area (using Siri remote)? I thought the panGesture touchType might help, but it didn't. Am I missing something obvious?


Solution

  • Set contentSize property to your scrollview. Make sure all components comes under given content size.

        myScrollView.contentSize = CGSizeMake(1880, 2000)