Scrollview, Inaccurate Auto Resizing?

Hello. So going through the documentation on scroll views, scroll views are supposed to auto adjust their bounds to compensate for the area of objects that are inserted into the scroll views. However, I notice that when I add display objects, the scroll view isn’t completely accurate in its auto adjustment.

For example:

http://imgur.com/0NSOm2H

When I scroll to the bottom of that scroll view, it should leave enough room for the 30th element, but for some reason, it cuts off part of element 29. This isn’t just happening to this scroll view, it is happening to most of the other scroll views in my game.

How should I fix my scroll views so they can properly display all of the content? Or is this just a Corona error?

Ok guys I’ve finally worked out the problem/solution of this dilemma.

Here is sample code to show you what’s happening:

[lua]

local widget = require(“widget”);

local s = widget.newScrollView {
        backgroundColor = { 0, 1, .3, .1 },
        x = display.contentCenterX,
        y = display.contentCenterY,
        width = display.contentWidth,
        height = display.contentHeight,
        horizontalScrollDisabled = false,
        verticalScrollDisabled = false
}

for i = 1, 100 do
    local text = tostring(i) … " element in the scroll view"
    local textLabel = display.newText(text, 0, (i+5) * 50, native.systemFont, 36);
    textLabel.anchorX, textLabel.anchorY = 0,1;
    s:insert(textLabel);

end

[/lua]

Here I create a regular scroll view, and insert 100 elements into the scroll view. However, I leave a buffer at the top of the scroll view, as can be seen in the following picture: http://imgur.com/me1CdNp,nN8BwsA,UCdCzz4#0

This buffer takes up approximately 5 elements of space.

When I scroll to the bottom, I should be able to see element 100; however, I can only see up to element 95.

http://imgur.com/me1CdNp,nN8BwsA,UCdCzz4#1

(To be clear, if you scroll past 95, you can still see 100, but if you let go of the scroll view, it bounces back to element 95. Run the code if you don’t know what I mean)

100 - 95 = 5 hidden elements

Buffer @ the top = 5 elements

Coincidence? I think not.

From this, I deduced that Corona set the scrollable height of the scroll view to the height of just the content in the scroll view. In my example, Corona set the scroll height to 100 elements (because there were 100 elements in the scroll view). However it failed to take into account the 5 element buffer at the top of the scroll view. Instead, it should have set the scroll height to 105 elements (to account for the extra buffer).

Is this what Corona is supposed to do? I think it would make more sense to include the height of the buffer into the recalculation of the scroll view’s scrollable height. If this is an error, I will report it (Btw, this error applies both top-down and left-right)

One quick solution for this dilemma is to add a ghost element to the top left corner of the scroll view. This way, Corona will not forget to factor in the height of the buffer zone:

[lua]

local widget = require(“widget”);

local s = widget.newScrollView {
        backgroundColor = { 0, 1, .3, .1 },
        x = display.contentCenterX,
        y = display.contentCenterY,
        width = display.contentWidth,
        height = display.contentHeight,
        horizontalScrollDisabled = false,
        verticalScrollDisabled = false
}

–Ghost element
local topLabel = display.newText("", 0, 0, native.systemFont, 36);
s:insert(topLabel);

for i = 1, 100 do
    local text = tostring(i) … " element in the scroll view"
    local textLabel = display.newText(text, 0, (i+5) * 50, native.systemFont, 36);
    textLabel.anchorX, textLabel.anchorY = 0,1;
    s:insert(textLabel);

end

[/lua]

All fixed! Now you can see element 100 at the bottom of the scroll view:

http://imgur.com/me1CdNp,nN8BwsA,UCdCzz4#2

Of course, this isn’t too hard to do, but it is rather annoying, so I hope Corona fixes this bug(?) soon!

Hello @atrizhong,

Can you please file a bug report with your simple project which shows this? We can then investigate the project and work on a resolution for it.

http://developer.coronalabs.com/content/bug-submission

Thanks,

Brent

Ok guys I’ve finally worked out the problem/solution of this dilemma.

Here is sample code to show you what’s happening:

[lua]

local widget = require(“widget”);

local s = widget.newScrollView {
        backgroundColor = { 0, 1, .3, .1 },
        x = display.contentCenterX,
        y = display.contentCenterY,
        width = display.contentWidth,
        height = display.contentHeight,
        horizontalScrollDisabled = false,
        verticalScrollDisabled = false
}

for i = 1, 100 do
    local text = tostring(i) … " element in the scroll view"
    local textLabel = display.newText(text, 0, (i+5) * 50, native.systemFont, 36);
    textLabel.anchorX, textLabel.anchorY = 0,1;
    s:insert(textLabel);

end

[/lua]

Here I create a regular scroll view, and insert 100 elements into the scroll view. However, I leave a buffer at the top of the scroll view, as can be seen in the following picture: http://imgur.com/me1CdNp,nN8BwsA,UCdCzz4#0

This buffer takes up approximately 5 elements of space.

When I scroll to the bottom, I should be able to see element 100; however, I can only see up to element 95.

http://imgur.com/me1CdNp,nN8BwsA,UCdCzz4#1

(To be clear, if you scroll past 95, you can still see 100, but if you let go of the scroll view, it bounces back to element 95. Run the code if you don’t know what I mean)

100 - 95 = 5 hidden elements

Buffer @ the top = 5 elements

Coincidence? I think not.

From this, I deduced that Corona set the scrollable height of the scroll view to the height of just the content in the scroll view. In my example, Corona set the scroll height to 100 elements (because there were 100 elements in the scroll view). However it failed to take into account the 5 element buffer at the top of the scroll view. Instead, it should have set the scroll height to 105 elements (to account for the extra buffer).

Is this what Corona is supposed to do? I think it would make more sense to include the height of the buffer into the recalculation of the scroll view’s scrollable height. If this is an error, I will report it (Btw, this error applies both top-down and left-right)

One quick solution for this dilemma is to add a ghost element to the top left corner of the scroll view. This way, Corona will not forget to factor in the height of the buffer zone:

[lua]

local widget = require(“widget”);

local s = widget.newScrollView {
        backgroundColor = { 0, 1, .3, .1 },
        x = display.contentCenterX,
        y = display.contentCenterY,
        width = display.contentWidth,
        height = display.contentHeight,
        horizontalScrollDisabled = false,
        verticalScrollDisabled = false
}

–Ghost element
local topLabel = display.newText("", 0, 0, native.systemFont, 36);
s:insert(topLabel);

for i = 1, 100 do
    local text = tostring(i) … " element in the scroll view"
    local textLabel = display.newText(text, 0, (i+5) * 50, native.systemFont, 36);
    textLabel.anchorX, textLabel.anchorY = 0,1;
    s:insert(textLabel);

end

[/lua]

All fixed! Now you can see element 100 at the bottom of the scroll view:

http://imgur.com/me1CdNp,nN8BwsA,UCdCzz4#2

Of course, this isn’t too hard to do, but it is rather annoying, so I hope Corona fixes this bug(?) soon!

Hello @atrizhong,

Can you please file a bug report with your simple project which shows this? We can then investigate the project and work on a resolution for it.

http://developer.coronalabs.com/content/bug-submission

Thanks,

Brent