Fit view height to its content

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

is it possible to fit view height to its content? When I set view height to 'auto' it takes 100% of window height

Thanks

4 Answers

var win = Titanium.UI.currentWindow;
 
var bg = Titanium.UI.createImageView({
    image: 'images/infos-bg.png',
    align: 'center',
    top: 0,
    width : '100%'
});
 
var accroche = Titanium.UI.createImageView({
    image: 'images/accroche.png',
    align: 'center',
    top: '35%',
    width : '75%'
});
 
var h = bg.toImage().height*0.7;
 
var sView = Titanium.UI.createView({
    width:'100%',
    top:-h,
    zIndex : 999,
    width:'100%',
    height: Ti.UI.SIZE,
    layout:'vertical'
});
 
var t = 0;
sView.addEventListener('swipe', function(){
    if(t==0){
        var anim1 = Ti.UI.createAnimation({
            top: 0,
            duration: 500
        });
        t=1;
    }else{
        var anim1 = Ti.UI.createAnimation({
            top: -h,
            duration: 500
        });
        t=0;
    }
    sView.animate(anim1);
});
 
sView.add(bg);
sView.add(accroche);
win.add(sView);

HI Mohamed

Set your height as this example;

height: Ti.UI.SIZE
That will make the height of the view match the height of the contents.

And;

height: Ti.UI.FILL
Will make the view fill to its parent height.

As long as you are using sdk 2.0 or higher - this works a charm.

Hi Mohamed

I have tried your code at the properties you are using seem to conflict with one another.

You have a 'parent' view called sView which has the layout property set to vertical. You then add two image views, bg and accroche to this view.

As you have set 'vertical' as the parent views layout, the two extra views will side one underneath the other.

But you have added a top parameters to one that uses a percentage value of the parent view, however the parent view determines its size based on its parent height.

You then add an animation to the swipe event move the parent view. You are also determining the height of the imageview bg for this animation.

I will be honest and say I am not sure what you are trying to achieve from this code.

Can you provide a description of what you want it to do, and then maybe I can help further.

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
3 Comments
  • Part of my view must be visible, the rest appears as soon as we swipe our finger on the first part.

    (sorry for my bad english)

    — commented 10 months ago by Mohamed El Bassir

  • Any reason why you are not using a scroll view as the parent view? Is this for Android, iPhone or iPad?

    Your images widths are both different, and both wider than the average screen size.

    If you can give me a bit more information I can hopefully resolve this for you.

    — commented 10 months ago by Malcolm Hollingsworth

  • My view is showing informations about the application, in the visible part I have a down arrow in witch we can swipe our finger in order to show the hidden part. I'm working actually on the iphone version.

    — commented 10 months ago by Mohamed El Bassir

Your Answer

Think you can help? Login to answer this question!