Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Webview: Auto height doesn't work

Hello

I have a webview in my app with above that webview a label and an imageview.

I made a mainView to hold the label, imageview and webview. Now when I put the webviews height on auto, i can't scroll (well i can scroll a little piece down, but it goes right back up when i release).

However when i only add my webview to my window (without the label and logo) the auto height works just fine.

I had the same problem with my scrollView, but i fixed that by making a var Height and adding all the heights of each label to it. I can't use that technique here because I don't know the height of the webview up front.

Can anyone help me with my problem?

I'm using Titanium SDK 1.6.1 and iPhone SDK 4.2

Here is a piece of my code:

var view = Titanium.UI.createView({
    backgroundColor: '#fff',
    layout: 'vertical'
});

var scrollView = Titanium.UI.createScrollView({
    contentWidth: 'auto',
    contentHeight: 'auto',

    top: 0,
    bottom: 0,

    showVerticalScrollIndicator: true,
    showHorizontalScrollIndicator: true,

    maxZoomScale: 100,
    minZoomScale: 0.1
});

var name = Titanium.UI.createLabel({
    text: boothHolder.fieldByName('Name'),
    textAlign: 'left',
    left: 'auto',
    top: 'auto',
    height: 'auto',
    width: 'auto',
    font: {
        fontSize: 15,
        fontFamily: 'Helvetica-Bold'
    }
});

view.add(name);

if (Titanium.Network.online) {
    var logo = Titanium.UI.createImageView({
        image: boothHolder.fieldByName('SmallLogoURL'),
        width: 'auto',
        preventDefaultImage: true,
        height: 'auto',
        bottom:10
    });

    view.add(logo);
};

var tekst = boothHolder.fieldByName('FormattedDescription');

var webView = Titanium.UI.createWebView({
        html: Encoder.htmlDecode(tekst),
        height:'auto',
        backgroundColor: '#fff'
});

height += webView.getHeight() + name.getHeight();

if (height > win.getHeight() - 20) {
    //Set height so scrollview scrolls
    scrollView.setContentHeight(height);
};

view.add(webView);

scrollView.add(view);
win.add(scrollView);

Thanks!

— asked April 5th 2011 by Tjeu Vdw
  • auto
  • height
  • mobile
  • problem
  • webview
0 Comments

3 Answers

  • I'm running into the same problem here.

    Did someone manage to get this fixed?

    Any help would be apreciated

    — answered June 24th 2011 by Greg Berger
    permalink
    0 Comments
  • Have the same Problem.

    Sinse the lase answer is 12 month ago I assume that the problem sill existst and there is no solution?

    — answered May 27th 2012 by Wolfgang Schmidetzki
    permalink
    0 Comments
  • Hey guys - I know this was over a year ago, but for anyone looking for the answer:

    In order to set the height of the webview to the height of its contents, the contents need to be first loaded. I recommend something like this:

    webview.html = 'Some HTML right here, or loaded by means of the url property';
    
    //I personally remove the webview from sight while I get the content loaded in there:
    webview.setOpacity(0);
    
    //We listen for the webview to fully load its HTML
    webview.addEventListener('load', function() {
    
      //Now with the webview fully loaded, this is the time to set the height accordingly:
      this.setHeight(Ti.UI.SIZE);
    
      //And now we can bring the webview into visibility:
      this.setOpacity(1);
    
    });
    
    — answered April 11th 2013 by Haseeb Qureshi
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.