Add a floating status bar on top of a scrollview like in Facebook app's timeline

You must Login before you can answer or comment on any questions.

Hi ! I've been trying to add a floating status bar on top of a scrollview like you see in Facebook app's timeline, so when you scroll down, the bar sits on top of the scrollview and scrolls with the scrollview, and when you scroll up, it scrolls up as well and stays behind the navbar.

I tried to achieve this by adding two views to a window with vertical layout, the upper view contains the status bar and the lower one scrollview, then in the scroll event callback I set the contentOffset.y to the 'top' properties of the status bar and the scrollview. But the result is not really what I want, the status bar has the exact behavior that I want, but it moves really laggy, am I doing this wrong?? How do I make it move smoothly?

Any help would be greatly appreciated! Thanks!

— asked 8 months ago by Andre Wu
0 Comments

1 Answer

Try this, this is just from the top of my head so might need some fixing here and there.

var t = Ti.UI.create2DMatrix();
 
var floatingView = Ti.UI.createView({
    width: Ti.UI.FILL,
    height: 60,
    backgroundColor: 'grey',
    inView: false,
    left: 0,
    top: 0,
    transform: t.translate(0, -60)
});
 
var scrollView = Ti.UI.createScrollView({
    ....
});
 
scrollView.addEventListener('scroll', function(e) {
    if(e.contentOffset.y > 60 && !floatingView.inView) {
        floatingView.animate({
            transform: t,
            duration: 300
        });
 
        floatingView.inView = true;
    }
    else if(e.contentOffset.y < 60 && floatingView.inView) {
        floatingView.animate({
            transform: t.translate(0, -60),
            duration: 300
        });
 
        floatingView.inView = false;
    }
});
 
win.add(scrollView);
win.add(floatingView);
 
/* Titanium considers the visual positions of the views in order to which they are added to the parentView */
This should work. The code is self-explanatory. If you got questions, fire away.

— answered 8 months ago by Nikhil Nigade
answer permalink
5 Comments
  • Forgot to mention this: By adding both the scrollView and the floatingView to a single parent (in this case, the window), you wouldn't need to constantly change the top property for the floatingView. This will eliminate the lag when scrolling, as the floatingView will always stay on top. Also, the window, in this case, shouldn't use a 'layout: vertical' property, or things will break.

    — commented 8 months ago by Nikhil Nigade

  • Thanks for your reply, using animate instead of setting the top property really makes it much smoother. But what I want is initially the floatingView will be seen on the screen sitting on top of the scrollView, in this case, if the floatingView's height is 60, I'll have to set the scrollView's top property to 60. The problem comes when I scroll, because when I animate the floatingView to -60, I need to animate the scrollView to 0 as well, and since the floatingView will leave a black rectangle with height 60 after it's animated, I need to add 60 to scrollView's height, which causes some of scrollView's content being cut off when scrolled to the end, I couldn't find a way to deal with this, could you help me out??

    — commented 8 months ago by Andre Wu

  • Check the properties contentOffset and the method setContentOffset. You'd want to set a value for the y-axis at something like 80. Use this instead of top.

    I'm mentioning both, since one of them animates the scrolling. It comes down to how you want it to look and function.

    — commented 8 months ago by Nikhil Nigade

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!