views:

330

answers:

1

So I have a bunch of UIViews that I made programmatically and placed a bunch of content on (UIButtons, UILabels, UINavigationBars, etc.), and most of these views are going to need to actually be UIScrollViews instead.

Now keep in mind I made all these through code not the Interface Builder. What I first tried was to just change each declaration of a UIView to a UIScrollView instead, and that compiled and ran fine, however, they were not scrollable. It's REALLY weird.

Here is the code that I change into UIScrollViews, although it doesn't scroll:

- (void)loadView {

     //allocate the view
     self.view = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
     //self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

     //set the view's background color
     self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"MainBG.jpg"]];

     [self.view addSubview:[SiteOneController myNavBar1:@"Sites"]];

     NSMutableArray *sites = [[NSMutableArray alloc] init];

     NSString *one = @"Constution Center";
     NSString *two = @"Franklin Court";
     NSString *three = @"Presidents House";

     [sites addObject: one];
     [one release];

     [sites addObject: two];
     [two release];

     [sites addObject: three];
     [three release];

     NSString *element;
     int j = 0;
     for (element in sites)
     {
         UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
         UIFont *myBoldFont = [UIFont boldSystemFontOfSize:20.0];
         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, b + (j*45), c, d)];
         [label setBackgroundColor:[UIColor clearColor]];

         label.text = element;
         label.font = myBoldFont;

         UIImage *img = [UIImage imageNamed:@"ButtonBase.png"];
         [button setImage:img forState:UIControlStateNormal];

         //setframe (where on screen)
         //separation is 15px past the width (45-30)
         button.frame = CGRectMake(a, b + (j*45), c, d);

         // [button setTitle:element forState:UIControlStateNormal];

         button.backgroundColor = [SiteOneController myColor1];

        [button addTarget:self action:@selector(showCCView:)
        forControlEvents:UIControlEventTouchUpInside];
         [button setTag:j];

         [self.view addSubview: button];
         [self.view addSubview:label];
         j++;

     }

 }

What am I doing wrong?

+2  A: 

Try setting a content size for the UIScrollView.

ie. scrollView.contentSize = CGSizeMake(320,760);

Eric Schweichler
Awesome. Thank you so much. I'll make a note of that.
Scott
One more question though. I probably made a mistake in my original question. This transforms the entire UIView to a UIScrollView. Which means now, when I scroll through even the nav bar scrolls with it...How do you make just the UIScrollView only take up the space above the tab bar but below the nav bar.
Scott
Assuming that `SiteOneController` is a UINavigationBarController you may want to change your code so that you add the UIScrollView inside the SiteOneController, that should fix your problem.
Eric Schweichler
SiteOneController is actually a UIViewController. I'm assuming if I add it inside this it will cover the nav bar at the top, but since the app is delegated by a TabBarController it would leave the tab bar untouched.
Scott
Well, your View with the NavigationBar is a child of the UITabBar, which is fine, so as long as the UIScrollView is a child of the ViewController with the NavBar, everything should work fine.UITabBarController -> UINavigationController -> UIScrollView. I have a similar setup in one of my apps, but I hide the TabBar Controller when the scroll view is present because I don't want the user to change tabs until they are done with the scroll view.
Eric Schweichler