Skip to content

Instantly share code, notes, and snippets.

@iskugor
Created December 21, 2011 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iskugor/1505988 to your computer and use it in GitHub Desktop.
Save iskugor/1505988 to your computer and use it in GitHub Desktop.
Titanium manual dimension calculation
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