Problem with vertical layout in a scrollView

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

Hi all,

I've been building my first mobile app using Appcelerator Titanium, and this forum has been very helpful thus far, but I have run into an issue I can't find an answer for so I decided to post it. So here goes.

I have a text window with scrollable, dynamic length content, so I need vertical layout in a ScrollView. However, I also need the window to close when user clicks on it. The problem is that the only way I seem to get the click (or singletap, doesn't matter which) to work on the window is to set touchenabled to false on all views inside it. When I do that, however, the ScrollView no longer seems to scroll. If I set the touchenabled of the scrollview to true, it starts scrolling again, but the click event on the window no longer fires. I have tried setting the click event on the scrollview instead, with no luck.

I have also tried using a wrapper view inside the scrollview (to move the layout: vertical from the scrollview to a different view), but still nothing.

This is all in iOS, using SDK 4.2.

The code I am using is as follows:

var textWindow = Ti.UI.createWindow({
    backgroundColor:'white',
    borderColor:    'black',
    borderWidth:    1,
    width:      '100%',
    height:     '100%',
    navBarHidden:   true
});
var scrollView = Ti.UI.createScrollView({
    layout:     'vertical',
    height:     '100%',
    contentWidth:   'auto',
    contentHeight:  'auto',
    showVerticalScrollIndicator: true,
    touchEnabled:   true
});
var titleLabel = Ti.UI.createLabel({
    left:       6,
    top:        6,
    height:     22,
    color:      'black',
    font:       { fontWeight:'bold', fontSize: fontSize + 2 },
    textAlign:  'left',
    text:       item.title,
    touchEnabled:   false
});
var textLabel = Ti.UI.createLabel({
    color:      'black',
    font:       { fontWeight:'normal', fontSize: fontSize },
    text:       item.text,
    textAlign:  'left',
    left:       6,
    right:      6,
    top:        6,
    height:     'auto',
    touchEnabled:   false
});
scrollView.add(titleLabel);
scrollView.add(textLabel);
textWindow.addEventListener('singletap', function(){
    var t3 = Ti.UI.create2DMatrix().scale(0);
    textWindow.close({transform:t3,duration:300});
});
textWindow.add(scrollView);
Any help would be greatly appreciated.

— asked 2 years ago by Rene Aavik
2 Comments
  • Should have mentioned, that with the above code, scrolling works but the tap event doesn't fire. Setting touchenabled to false on scrollView fixes the event, but scrolling no longer works.

    — commented 2 years ago by Rene Aavik

  • I'm not sure what exactly you're asking, can you restate the question in 1 or 2 sentences?

    — commented 2 years ago by Joe iEntry

2 Answers

Hmm, I just had a problem with a ScrollView not showing up. Not entirely sure if I'm on-topic but a ScrollView apparently is not displayed when vertical layout is enabled in the current window (or maybe it's way off the viewport).

In a window with absolute layout everything is fine. Maybe this helps.

I kind think of a reason why one would have a vertical layout when using a ScrollView (i made a little mistake while "refactoring" so i still had it vertical and was wondering why my content disappeared).

— answered 2 years ago by Paul Voss
answer permalink
1 Comment
  • The reason I need vertical layout is because I have a dynamic length text field in the ScrollView. If I use absolute layout, and htere is less text than there is space, it will center the text vertically, instead of aligning it to the top of the page (under title). The only solution I managed to figure out to this problem was using layout: vertical.

    — commented 2 years ago by Rene Aavik

Instead of adding the eventlistener to the window, you will need to add it to one of the elements on it. Usually you will add a back button, to close the window but in your case you can do this:

scrollView.addEventListener('singletap', function(){
    var t3 = Ti.UI.create2DMatrix().scale(0);
    textWindow.close({transform:t3,duration:300});
});

— answered 2 years ago by Daniel Tome
answer permalink
2 Comments
  • Thank you for the answer, however, if you look at my post more carefully, you will notice that I already tried this: > I have tried setting the click event on the scrollview instead, with no luck.

    — commented 2 years ago by Rene Aavik

  • Oh, and I did eventually fix the issue by using the back button, as you suggested. This was not ideal in my case, but served as a workaround.

    — commented 2 years ago by Rene Aavik

Your Answer

Think you can help? Login to answer this question!