Created
December 21, 2011 13:09
-
-
Save iskugor/1505988 to your computer and use it in GitHub Desktop.
Titanium manual dimension calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var win = Ti.UI.createWindow({ | |
layout: 'vertical', | |
backgroundColor: '#000' | |
}); | |
var view = Ti.UI.createView({ | |
backgroundColor: '39c', | |
//height: '70%', | |
layout: 'vertical' | |
}); | |
view.sizeCalculated = {}; | |
var b1 = Ti.UI.createButton({ | |
height: 100, | |
top: 0, | |
title: 'Set fixed' | |
}); | |
b1.addEventListener('click', function() { | |
Ti.API.info('Setting fixed value ...'); | |
//set view's value | |
view.height = 500; | |
//calculate view's value | |
view.sizeCalculated.height = 500; | |
}); | |
var b2 = Ti.UI.createButton({ | |
height: 100, | |
top: 0, | |
title: 'Set percentage' | |
}); | |
b2.addEventListener('click', function() { | |
Ti.API.info('Setting percentage value ...'); | |
view.height = '50%'; | |
//calculation | |
var percentageNumber = +view.height.slice(0, view.height.length - 1) / 100; | |
view.sizeCalculated.height = Math.floor(view.parentElement.size.height * percentageNumber); | |
}); | |
var b3 = Ti.UI.createButton({ | |
height: 100, | |
top: 0, | |
title: 'Set auto' | |
}); | |
b3.addEventListener('click', function() { | |
Ti.API.info('Setting auto value ...'); | |
view.height = 'auto'; | |
//calculation | |
var viewHeight = 0; | |
for (var i = 0; i < view.children.length; ++i) { | |
viewHeight += view.children[i].size.height; | |
} | |
view.sizeCalculated.height = viewHeight; | |
}); | |
var b4 = Ti.UI.createButton({ | |
height: 100, | |
top: 0, | |
title: 'Get calculated' | |
}); | |
b4.addEventListener('click', function() { | |
Ti.alert('TiCalc: ' + view.size.height); | |
Ti.alert('MyCalc: ' + view.sizeCalculated.height); | |
}); | |
view.add(b1); | |
view.add(b2); | |
view.add(b3); | |
view.add(b4); | |
win.add(view); | |
view.parentElement = win; | |
win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment