scrollWidth and scrollHeight in scrollView not working?

I had the exact some problem. 

I solved it by calling the scrollView:setScrollWidth (or Height) after inserting the objects. 

Piece of Cake!!!

Can confirm that nickwhite125 is 100% correct.  I was calling the :setScrollHeight BEFORE inserting the objects but if you call it AFTER inserting the objects it works like a charm.  Looking back and after struggling for 30 mins I guess it is a ‘Piece of Cake!!’ :slight_smile:

Hi all,

i’m currently developing a page which I want them (including the buttons) to scroll. But it seems that it doesn’t work. I dont know where is my mistake(s). The listener works and prompted the scripts as coded. Below is the sample of my coding:

------------------------------------------------------------------- -- layout.lua ------------------------------------------------------------------- local composer = require( "composer" ) local widget = require ( "widget" ) local scene = composer.newScene() -- forward declarations and other locals local background, scrollView, backButton, homeButton local swipeThresh = 100 -- amount of pixels finger must travel to initiate page swipe local tweenTime = 500 local animStep = 1 local readyToContinue = false -- function to show next animation -- -- -- local function scrollListener( event ) local phase = event.phase if ( phase == "began" ) then print( "Scroll view was touched" ) elseif ( phase == "moved" ) then print( "Scroll view was moved" ) elseif ( phase == "ended" ) then print( "Scroll view was released" ) end -- In the event a scroll limit is reached... if ( event.limitReached ) then if ( event.direction == "up" ) then print( "Reached bottom limit" ) elseif ( event.direction == "down" ) then print( "Reached top limit" ) elseif ( event.direction == "left" ) then print( "Reached right limit" ) elseif ( event.direction == "right" ) then print( "Reached left limit" ) end end return true end local function backButtonRelease( self, event ) -- body if event.phase == "ended" or event.phase == "cancelled" then composer.gotoScene("layout", "slideRight", 800) return true end end local function homeButtonRelease( self, event ) -- body if event.phase == "ended" or event.phase == "cancelled" then composer.gotoScene("menu", "slideDown", 800) return true end end function scene:create( event ) local sceneGroup = self.view -- Create the widget scrollView = widget.newScrollView { top = 100, left = 10, width = display.contentWidth, height = display.contentHeight, scrollWidth = display.contentWidth, scrollHeight = display.contentHeight, topPadding = 50, bottomPadding = 50, horizontalScrollDisabled = true, listener = scrollListener } -- Create a image and insert it into the scroll view background = display.newImageRect( "cool\_house\_bg.png", display.contentWidth, display.contentHeight ) background.anchorX = 0 background.anchorY = 0 scrollView:insert( background ) backButton = widget.newButton { left = -2, top = -4, width = 150, height = 100, defaultFile = "buttonBack.png", onRelease = backButtonRelease, } scrollView:insert( backButton ) homeButton = widget.newButton { left = 630, top = -5, width = 150, height = 100, defaultFile = "buttonHome.png", onRelease = backButtonRelease, } scrollView:insert( homeButton ) -- Called when the scenes view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. sceneGroup:insert( scrollView ) sceneGroup:insert( background ) sceneGroup:insert( backButton ) sceneGroup:insert( homeButton ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. backButton.touch = backButtonRelease backButton:addEventListener( "touch", backButton ) homeButton.touch = homeButtonRelease homeButton:addEventListener( "touch", homeButton ) scrollView.touch = scrollListener scrollView:addEventListener( "touch", scrollView) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) -- remove event listener from background backButton:removeEventListener( "touch", backButton ) homeButton:removeEventListener( "touch", homeButton ) scrollView:removeEventListener( "touch", scrollView ) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scenes "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

Really hope someone can help me with this :(  :(  :frowning:

Khairul

Your code here:

    sceneGroup:insert( scrollView )

    sceneGroup:insert( background )
    sceneGroup:insert( backButton )
    sceneGroup:insert( homeButton )

Is adding the buttons to the scene group, which is taking them out of the scrollview that you just put them in. The buttons can only be in one parent, the sceneGroup or the scrollview, but not both.

Hope this helps.

Code should be:

    sceneGroup:insert( scrollView )

    sceneGroup:insert( background )
    sceneGroup:insert( backButton )
    sceneGroup:insert( homeButton )

Thank you redpistonCrna, it works wonderfully… But now I got a new situation. The buttons (home and back buttons) doesn’t work…

What do I have to do in order for the buttons to work and at the same time it stays in the scrollview function?

Khairul

Can confirm that nickwhite125 is 100% correct.  I was calling the :setScrollHeight BEFORE inserting the objects but if you call it AFTER inserting the objects it works like a charm.  Looking back and after struggling for 30 mins I guess it is a ‘Piece of Cake!!’ :slight_smile:

Hi all,

i’m currently developing a page which I want them (including the buttons) to scroll. But it seems that it doesn’t work. I dont know where is my mistake(s). The listener works and prompted the scripts as coded. Below is the sample of my coding:

------------------------------------------------------------------- -- layout.lua ------------------------------------------------------------------- local composer = require( "composer" ) local widget = require ( "widget" ) local scene = composer.newScene() -- forward declarations and other locals local background, scrollView, backButton, homeButton local swipeThresh = 100 -- amount of pixels finger must travel to initiate page swipe local tweenTime = 500 local animStep = 1 local readyToContinue = false -- function to show next animation -- -- -- local function scrollListener( event ) local phase = event.phase if ( phase == "began" ) then print( "Scroll view was touched" ) elseif ( phase == "moved" ) then print( "Scroll view was moved" ) elseif ( phase == "ended" ) then print( "Scroll view was released" ) end -- In the event a scroll limit is reached... if ( event.limitReached ) then if ( event.direction == "up" ) then print( "Reached bottom limit" ) elseif ( event.direction == "down" ) then print( "Reached top limit" ) elseif ( event.direction == "left" ) then print( "Reached right limit" ) elseif ( event.direction == "right" ) then print( "Reached left limit" ) end end return true end local function backButtonRelease( self, event ) -- body if event.phase == "ended" or event.phase == "cancelled" then composer.gotoScene("layout", "slideRight", 800) return true end end local function homeButtonRelease( self, event ) -- body if event.phase == "ended" or event.phase == "cancelled" then composer.gotoScene("menu", "slideDown", 800) return true end end function scene:create( event ) local sceneGroup = self.view -- Create the widget scrollView = widget.newScrollView { top = 100, left = 10, width = display.contentWidth, height = display.contentHeight, scrollWidth = display.contentWidth, scrollHeight = display.contentHeight, topPadding = 50, bottomPadding = 50, horizontalScrollDisabled = true, listener = scrollListener } -- Create a image and insert it into the scroll view background = display.newImageRect( "cool\_house\_bg.png", display.contentWidth, display.contentHeight ) background.anchorX = 0 background.anchorY = 0 scrollView:insert( background ) backButton = widget.newButton { left = -2, top = -4, width = 150, height = 100, defaultFile = "buttonBack.png", onRelease = backButtonRelease, } scrollView:insert( backButton ) homeButton = widget.newButton { left = 630, top = -5, width = 150, height = 100, defaultFile = "buttonHome.png", onRelease = backButtonRelease, } scrollView:insert( homeButton ) -- Called when the scenes view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. sceneGroup:insert( scrollView ) sceneGroup:insert( background ) sceneGroup:insert( backButton ) sceneGroup:insert( homeButton ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. backButton.touch = backButtonRelease backButton:addEventListener( "touch", backButton ) homeButton.touch = homeButtonRelease homeButton:addEventListener( "touch", homeButton ) scrollView.touch = scrollListener scrollView:addEventListener( "touch", scrollView) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) -- remove event listener from background backButton:removeEventListener( "touch", backButton ) homeButton:removeEventListener( "touch", homeButton ) scrollView:removeEventListener( "touch", scrollView ) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scenes "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene

Really hope someone can help me with this :(  :(  :frowning:

Khairul

Your code here:

    sceneGroup:insert( scrollView )

    sceneGroup:insert( background )
    sceneGroup:insert( backButton )
    sceneGroup:insert( homeButton )

Is adding the buttons to the scene group, which is taking them out of the scrollview that you just put them in. The buttons can only be in one parent, the sceneGroup or the scrollview, but not both.

Hope this helps.

Code should be:

    sceneGroup:insert( scrollView )

    sceneGroup:insert( background )
    sceneGroup:insert( backButton )
    sceneGroup:insert( homeButton )

Thank you redpistonCrna, it works wonderfully… But now I got a new situation. The buttons (home and back buttons) doesn’t work…

What do I have to do in order for the buttons to work and at the same time it stays in the scrollview function?

Khairul