将UIViews以编程方式转换为UIScrollViews

时间:2010-03-18 23:10:11

标签: objective-c iphone uiview uiscrollview

所以我有一堆UIViews我以编程方式制作并在(UIButtonsUILabelsUINavigationBars等上放置了大量内容,并且大多数这些视图实际上需要UIScrollViews代替。

现在请记住,我通过代码而不是Interface Builder完成了所有这些。我首先尝试的是将UIView的每个声明更改为UIScrollView,然后编译并运行正常,但是,它们不可滚动。这真的很奇怪。

以下是我更改为UIScrollViews的代码,虽然它不会滚动:

- (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++;

     }

 }

我做错了什么?

1 个答案:

答案 0 :(得分:2)

尝试为UIScrollView设置内容大小。

即。 scrollView.contentSize = CGSizeMake(320,760);