views:

815

answers:

2

Alright, so the key here is I'm not using IB at all, because the View I'm working with is created programmatically. The UIView covers the lower half the screen, and has a bunch of buttons on it. However, I want to add more buttons to the UIView, without making it any larger. To do so, I want to make a UIScrollView inside the view, which will allow me to add more buttons off screen so the user can scroll to them. I think that's how it works.

self.manaView = [[[UIView alloc] initWithFrame:frame] autorelease];
self.manaView.backgroundColor = [UIColor purpleColor];

UIScrollView *scroll = [UIScrollView alloc];
scroll.contentSize = CGSizeMake(320, 400);
scroll.showsHorizontalScrollIndicator = YES;
[self.manaView addSubview:scroll];

The first part of the code iniates my UIView, which works great, but I can't figure out how to make the UIScrollView programmatically and add it to the view, and then add the buttons to it.

UIButton *ret2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ret2.tag = 102;
ret2.frame = CGRectMake(255, 5, 60, 50);
[ret2 setTitle:@"Return" forState:UIControlStateNormal];
[ret2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:ret2];

When I did that, the button simply disappeared off my screen. So How do I do this correctly? Thank you for your help!

A: 

Instead of:

UIScrollView *scroll = [UIScrollView alloc];

do this (setting the frame to however big you want the scroll view to be):

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...];
Ole Begemann
Ahhh, I had to have initwithframe AND .contentSize to have it work. Thank you!
Wayfarer
A: 

i'm having the same bit of trouble. how did you end up implementing it? i did this with no success, putting this code inside my viewDidLoad ::

CGRect scrollContainer = CGRectMake(0, 0, 320, 45);
UIScrollView *scrollMenu = [[UIScrollView alloc] initWithFrame:scrollContainer];
scrollMenu.contentSize = CGSizeMake(320, 45);
scrollMenu.showsHorizontalScrollIndicator = NO;
[self.view addSubview:scrollMenu];

i also get a bunch of warnings saying 'local declaration of 'scrollMenu' hides instance variable'. anyway, i don't have any scrolling happening. if i change the button locations i can see them on screen, but no scroll. guidance is appreciated! thanks mate.

Chunjai
got rid of the warnings. it was supposed to be ... (UIScrollView *) scrollMenu ... i was overriding a variable. still wondering about scrolling though.
Chunjai
hahaha, figured it out. in 'CGSizeMake(320,45);' i had no room for scrolling. changed it to (500, 45) and vuola, sidescrolling happiness. thanks for the post mate.
Chunjai
Wonderful! Glad it worked out for you!
Wayfarer