Skip to content

Instantly share code, notes, and snippets.

@balanza
Created January 19, 2015 12:19
Show Gist options
  • Save balanza/c396ac1f5013fd586ce4 to your computer and use it in GitHub Desktop.
Save balanza/c396ac1f5013fd586ce4 to your computer and use it in GitHub Desktop.
Show/Hide views when layout=vertical in Titanium
/*
* Snippet to solve a common problem in Titanium:
* hiding a view inside a vertical layout
* Tested with Alloy, will work on plain-vanilla Titanium too (just check underscore reference)
*/
/**
* hides a view nested into a layout=vertical container
* acts on top, bottom and height to simulate html-style display:none
* @param {Ti.View} view the view to be hidden
*/
function hideVertical(view) {
//store previous values
view.__originalValues = {
top: view.top,
bottom: view.bottom,
height: view.height
};
//set new values to simulate invisibility
view.top = 0;
view.bottom = 0;
view.height = 0;
view.visible = false;
}
/**
* shows a view nested into a layout=vertical container
* restore from hideVertical()
* @param {Ti.View} view the view to be shown
*/
function showVertical(view) {
//restore previous values
view = _.extend(view, view.__originalValues || {});
view.visible = true;
}
@gjerlow
Copy link

gjerlow commented Mar 1, 2018

Great tip @ShawnCBerg. How about using applyProperties() on the view in the hide function to reduce the over-the-bridge calls?

@icecandy
Copy link

applyProperties isn't necessarily optimal apparently...
https://shockoe.com/development/profiling-titanium

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment