LAccordion for iOS

Hey, so I’m finally up and running with my accordion for ios. You can find the code on my git LAccordion. There is some setup with those buttons and item that have subitems, but its quite simple. The main task was to correctly calculate the offset of items and their height. This is the code that runs on button click:

-(void) moveAndCollapseItemsWith:(Item*)object{

	Item *i = [items objectAtIndex:0];
	float offset = i.frame.origin.y;
	int height = defHeight;
	int index = 0;

	for(Item* item in items){
		if(index > 0){
			Item *i = [items objectAtIndex:index-1];
			offset = i.frame.origin.y+padding;
			if([i isEqual:object]){
				if(i.status == STATUS_EXPANDED)
					offset += ([i.subItems count]) * subItemsPadding+(subItemsPadding/2);

		if([item isEqual:object]){
			if(item.status == STATUS_EXPANDED)
				height = ([item.subItems count]+1) * subItemsPadding+(subItemsPadding);
			else height = defHeight;
		} else {
			item.status = STATUS_COLLAPSED;
			height = defHeight;

		[UIView animateWithDuration:0.5f
							 item.frame = CGRectMake(item.frame.origin.x, offset, item.frame.size.width, height);
							 item.bottomBorder.frame = CGRectMake(0, height-2, 320, 2);
						 } completion:^(BOOL finished){



First thing we do is setting the item status to expanded or collapsed. Then we iterate through all items in list, and if it’s first item we calculate its height with no offset, for each other item we take the previous one and calculate its height to get current item offset. Rest in code 😀

About the author


Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *

Możesz użyć następujących tagów oraz atrybutów HTML-a: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>