iphone - Dynamic UIButtons with vertical spacing on a UIScrollView -
i need space uibuttons out on uiscrollview. code have works, depending on amount of dataitems
have, spacing not even.
snippet in question
cgrectmake(10, ((120 / (count + 1)) * (i + 1) * 3) ,300,50)
specifically
((120 / (count + 1)) * (i + 1) * 3)
working code
int count = [dataitems count]; /* not specific value, can grow */ (int = 0; < count; i++) { uibutton* abutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; [abutton settag:i]; [abutton setframe: cgrectmake(10,((120 / (count + 1)) * (i + 1) * 3) ,300,50) ]; [abutton settitle:[[dataitems objectatindex:i] objectforkey:@"feed"] forstate:uicontrolstatenormal]; [abutton addtarget:self action:@selector(viewcategories:) forcontrolevents:uicontroleventtouchupinside]; [scroller addsubview:abutton]; }
screenshot
the example on right should example on left far spacing goes. uibuttons
sitting on uiscrollview
, uiscrollview
's contentsize
should grow if there more dataitems
buttons can scroll offscreen if there say, 30+ dataitems
.
sounds need set size , padding...
int count = [dataitems count]; cgfloat staticx = 10; // static x buttons. cgfloat staticwidth = 300; // static width buttons. cgfloat staticheight = 50; // static height buttons. cgfloat staticpadding = 10; // padding add between each button. (int = 0; < count; i++) { uibutton* abutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; [abutton settag:i]; //calculate static values. //i added 1 set of padding top. multiplied padding plus height count. // 10 + 0 first // 10 + 60 second. // 10 + 120 third. , on. [abutton setframe: cgrectmake(10,(staticpadding + (i * (staticheight + staticpadding)) ,staticwidth,staticheight) ]; [abutton settitle:[[dataitems objectatindex:i] objectforkey:@"feed"] forstate:uicontrolstatenormal]; [abutton addtarget:self action:@selector(viewcategories:) forcontrolevents:uicontroleventtouchupinside]; [scroller addsubview:abutton]; }
however if trying buttons sort of behavior. inclined agree rest.. can make custom cell button in it.
then can load button cell when table asks button.
and have table tie [dataitems count] total of items.
then calculation set cell height when set dataitems count. making sure not make them short. , table handle rest.
Comments
Post a Comment