转换UIViews做编程到UIScrollViews

问题描述:

所以我有一堆UIViews我编程制作并放置了一堆的内容上(UIButtonsUILabelsUINavigationBars等),大多数的这些意见都将需要实际上是代替UIScrollViews转换UIViews做编程到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++; 

    } 

} 

我在做什么错?

尝试设置UIScrollView的内容大小。

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

+0

太棒了。非常感谢。我会记下这一点。 – Scott 2010-03-19 00:38:01

+0

但还有一个问题。我可能在我原来的问题上犯了一个错误。这将整个UIView转换为UIScrollView。这意味着现在,当我滚动甚至导航栏滚动它...你如何使UIScrollView只占用标签栏上方的空间,但在导航栏下方。 – Scott 2010-03-19 01:43:09

+0

假设'SiteOneController'是一个UINavigationBarController,您可能需要更改代码,以便在SiteOneController中添加UIScrollView,以解决您的问题。 – 2010-03-19 02:01:08