Hi Corona Community,
I am developing an app, and for one of the scenes I would like to nest a horizontal ScrollView within a vertical ScrollView.
Now according to the official documentation, there should be no problems nesting ScrollViews, as long as the nesting count doesn’t exceed 3. Notwithstanding, I’ve been encountering problems, and I’m only trying to nest one ScrollView into another (for a nesting count of 2).
Please find the code below, a boiled-down version of what I am trying to do. The code complies without any errors, so you can plug it into your favorite platform and see for yourself.
display.setStatusBar(display.HiddenStatusBar) local widget = require("widget") local CX, CY = display.contentCenterX, display.contentCenterY local title local horizontal1Text, horizontal2Text, horizontal3Text local horizontal1, horizontal2, horizontal3 local vertical1Text, vertical2Text, vertical3Text local vertical1, vertical2, vertical3 local scrollView, verticalScrollView, horizontalScrollView title = display.newText("Scroll View Sucks", CX, 100, native.systemFont, 60) title:setFillColor(1) vertical1Text = display.newText("Ver Obj 1", CX, 200) vertical1Text:setFillColor(1) vertical1 = display.newRect(CX, 300, 100, 100) vertical1:setFillColor(1,0,1) vertical2Text = display.newText("Ver Obj 2", CX, 500) vertical2Text:setFillColor(1) vertical2 = display.newCircle(CX, 600, 55) vertical2:setFillColor(0,0,1) horizontal1Text = display.newText("Hrz Obj 1", 100, 800) horizontal1Text:setFillColor(1) horizontal1 = display.newRect(100, 900, 100, 100) horizontal1:setFillColor(1,1,0) horizontal2Text = display.newText("Hrz Obj 2", 400, 800) horizontal2Text:setFillColor(1) horizontal2 = display.newCircle(400, 900, 55) horizontal2:setFillColor(0,1,1) horizontal3Text = display.newText("Hrz Obj 3", 700, 800) horizontal3Text:setFillColor(1) horizontal3 = display.newRect(700, 900, 100, 100) horizontal3:setFillColor(1,0,0) vertical3Text = display.newText("Ver Obj 3", CX, 1100) vertical3Text:setFillColor(1) vertical3 = display.newCircle(CX, 1200, 55) vertical3:setFillColor(0,1,0) function initHorizontalScrollView() horizontalScrollView = widget.newScrollView { left = 0, top = 0, width = display.contentWidth, height = display.contentHeight, horizontalScrollDisabled = false, verticalScrollDisabled = true, hideBackground = true } horizontalScrollView:insert(title) horizontalScrollView:insert(horizontal1Text); horizontalScrollView:insert(horizontal1) horizontalScrollView:insert(horizontal2Text); horizontalScrollView:insert(horizontal2) horizontalScrollView:insert(horizontal3Text); horizontalScrollView:insert(horizontal3) end function initVerticalScrollView() verticalScrollView = widget.newScrollView { left = 0, top = 0, width = display.contentWidth, height = display.contentHeight, horizontalScrollDisabled = true, verticalScrollDisabled = false, hideBackground = true } verticalScrollView:insert(title) verticalScrollView:insert(vertical1Text); verticalScrollView:insert(vertical1) verticalScrollView:insert(vertical2Text); verticalScrollView:insert(vertical2) verticalScrollView:insert(vertical3Text); verticalScrollView:insert(vertical3) --verticalScrollView:insert(horizontalScrollView) end function initScrollView() scrollView = widget.newScrollView { left = 0, top = 0, width = display.contentWidth, height = display.contentHeight, horizontalScrollDisabled = false, verticalScrollDisabled = false, hideBackground = true } ---[[scrollView:insert(title) scrollView:insert(vertical1Text); scrollView:insert(vertical1) scrollView:insert(vertical2Text); scrollView:insert(vertical2) scrollView:insert(vertical3Text); scrollView:insert(vertical3) scrollView:insert(horizontal1Text); scrollView:insert(horizontal1) scrollView:insert(horizontal2Text); scrollView:insert(horizontal2) scrollView:insert(horizontal3Text); scrollView:insert(horizontal3) --]] --[[scrollView:insert(horizontalScrollView) scrollView:insert(verticalScrollView) --]] end initHorizontalScrollView() initVerticalScrollView() --initScrollView()
As I said above, the goal is to place all the “horizontal” objects into a horizontal ScrollView, and then put everything into a larger vertical ScrollView.
This sounds like a pretty straight-forward task.
However, when you call initVerticalScrollView( ) after calling initHorizontalScrollView( ), the previously created horizontal scroll view is destroyed (i.e. you can’t pan horizontally anymore). In a similar fashion, the vertical scroll view is destroyed when the order of functions being called is reversed.
This has left me thinking that no more than 1 scroll view can exist at any time.
So I tried creating a scroll view that allows both horizontal and vertical scrolling, and stuffed all the objects in it. Comment out initVerticalScrollView( ) and initHorizontalScrollView( ), and uncomment initScrollView( ) to see the effect. As you can see, when you try to pan horizontally, all the contents, including objects that are not intended to do so, pan too!
I also tried adding both verticalScrollView and horizontalScrollView to a scrollView, but then nothing scrolls anymore.
So, basically, nothing has worked so far and I am out of ideas. I think it is pretty clear I am trying to achieve here, and am surprised to learn that Corona doesn’t support this not-so-complicated task.
I am being silly here and missing something very obvious, or is what am I trying to do impossible? If you believe the former is the case, please tell me where I went wrong and possibly include code as well.
Thanks a lot in advance