Convert UIViews made programmatically to UIScrollViews

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2010-03-18T23:10:11Z Indexed on 2010/03/19 0:31 UTC
Read the original article Hit count: 432

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?

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about objective-c