ios - Scroll a view with 20 text boxes horizontally using UIScrollView -
i have implement 20 text boxes horizontally. how can add scroll bar see text boxes when scroll left.
you can find plenty of questions topic (i.e., calculate contentsize of scrollview), nonetheless here solution:
you want use uiscrollview
task. using rather simple in fact.
first need initialise , display uiscrollview
somewhere in screen. going assume in uiviewcontroller
, , want create scroll view of 300px width
, 200px height
. this, can following:
uiscrollview *ascrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0, 0, 300, 200)]; [[self view] addsubview:ascrollview]; [ascrollview setscrollenabled:yes];
now time place content inside of it. key idea here take consideration following. while 'uiscrollview' has size itself, has internal size it's content, scrollable. let's suppose have 20 items, each of has size of '40px width', , '200px height'. going suppose want leave space of 10px between each text box. thus, total scrollable width area want have inside 'uiscrollview' following: (20 (items) * (40 (size of each item) + 10 (distance between each item))) -10. shall following:
[ascrollview setcontentsize:cgsizemake((20*(40 + 10))-10), 200];
all right, done. have add each of text items scroll view. instead of doing 1 one, going assume have them inside collection such nsarray
(probably nsmutablearray
).
nsarray *textfields; // array collection of text boxes int x = 0; (uitextfield *atextfield in textfields){ [atextfield setframe:cgrectmake(x, 0, 40, 200)]; [[self view] addsubview:atextfield]; x += 40 + 10; // incrementing x coordinate correctly place next item }
that's it!
Comments
Post a Comment