Ability to always display ScrollBar in ui::Scrollview

First off, I love the flexibility of the ui::ScrollView! My project is some years old, and I’ve been using the extension version, but yesterday decided to try out the 3.16 ui::ScrollView in a small dialog, and it’s great!

What I’m doing is setting the scrollview contentSize, and then adding my content, keeping track of my total vertical space, and then setting the innerContainerSize at the end, using my height, and it’s working like a charm.

Scrolling also works wonderfully, and the mouse is great on Windows.

However, what I’d like to do is detect if my total content size (the innerView) is taller than my scrollview, and if so, show the scrollbar in order to indicate to the user that they can scroll.

I’ve done this, but the scrollbar is still initially hidden:
scrollView->setScrollBarEnabled(true);
scrollView->setScrollBarAutoHideEnabled(false);

I’ve also dabbled with the opacity, but it simply starts out hidden. Is there a way to have it start out visible if appropriate?

thanks!

Remove this line and add desired color to ScrollBar.

msgChatScroll->setScrollBarColor(Color3B(255, 255, 255));
msgChatScroll->setScrollBarAutoHideEnabled(false);
//msgChatScroll->setScrollBarEnabled(true);

Thank you for this, but strangely, my implementation seems to be ignoring the color and also not displaying automatically. I’m using cocos2d-x 3.16, and here’s my code:

  auto scrollView = ui::ScrollView::create();
  scrollView->setScrollBarColor(CCCOLOR_BLACK);
  scrollView->setScrollBarAutoHideEnabled(false);

  scrollView->setClippingEnabled(true);
  scrollView->setContentSize(Size(contentWidth, contentHeight));
  scrollView->setDirection(ui::ScrollView::Direction::VERTICAL);
  scrollView->getInnerContainer()->setLayoutType(ui::Layout::Type::VERTICAL);

  // we should now have the total height of the inner container
  scrollView->setInnerContainerSize(Size(contentWidth,totalHt));

  // move the scrollbar out a little closer to the right side
  auto distance = scrollView->getScrollBarPositionFromCornerForVertical();
  distance.x = ADJUST_X(6);
  scrollView->setScrollBarPositionFromCornerForVertical(distance);

thanks again!

Ok, this helped make it visible, but I may not want that, unless I can get the scroll bar to move when I scroll the mouse. So I’m going to google for that as well.

Leaving this here, in case it helps anyone else. It does require modifying a cocos2d-x class.

Try removing this line also.
This is my code, we are using in many places without any issue.

    ui::ScrollView *msgChatScroll = ui::ScrollView::create();
	msgChatScroll->setBounceEnabled(true);
	msgChatScroll->setContentSize(Size(550, 680));
	msgChatScroll->setDirection(ui::ScrollView::Direction::VERTICAL);
	msgChatScroll->setPosition(Vec2(viewSize.width / 2 - 270, viewSize.height / 2 - 370));
	profEditLayer->addChild(msgChatScroll);
	msgChatScroll->setScrollBarColor(Color3B(255, 255, 255));
	msgChatScroll->setScrollBarAutoHideEnabled(false);

	float innerWidth = msgChatScroll->getContentSize().width;
	float innerHeight = (noOfItem *  gap);
	msgChatScroll->setInnerContainerSize(Size(innerWidth, innerHeight));
1 Like

Thanks! I’ll look at that this evening. Thanks much!