2013-12-19 123 views
0

我想在iOS中水平滾動的單個視圖中創建多個UIScrollViews。這是到目前爲止我的代碼:UIScrollView不滾動iOS

-(void)updateSection { 
    [feedLoadingActInd stopAnimating]; 
    feedLoadingActInd.hidden = YES; 
    builder = [NSMutableArray array]; 
    float xPosition = 0; 
    float xPosBut = 0; 

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 29)]; 
    [scrollView setScrollEnabled:YES]; 
    scrollView.backgroundColor = [UIColor yellowColor]; 

    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    scrollView.pagingEnabled = YES; 
    scrollView.delegate = self; 

    for (int i = 0; i < itemArticleArray.count; i++) { 
     testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosBut, 40, 40, 40)]; 
     [testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; 
     [testButton setTitle:@"Test" forState:UIControlStateNormal]; 
     [testButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 
     testButton.backgroundColor = [UIColor blueColor]; 
     xPosBut += testButton.frame.size.width; 
     NSLog(@"scroll.frame.size.width = %f", scrollView.frame.size.width); 

     xPosition += 2; 
     UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 350, scrollView.frame.size.height - 8)]; 
     seperatorView.backgroundColor = [UIColor redColor]; 
     [scrollView addSubview:seperatorView]; 
     xPosition += 350; 

     [seperatorView addSubview:testButton]; 
     [scrollView addSubview:seperatorView]; 
     [builder addObject:testButton]; 

    } 
    [self addSubview:scrollView]; 
    [scrollView setUserInteractionEnabled:YES]; 
    [scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)]; 

    NSLog(@"scroll.contentsize.width = %f", scrollView.contentSize.width); 

}

然而,沒有任何的滾動意見實際上是滾動的,我感到困惑,因爲有添加多個按鈕。另外,我添加的按鈕在按下按鈕時並沒有做任何事情。他們應該運行buttonPressed方法,而不是?

任何幫助將不勝感激!

+0

您正在將滾動視圖內容大小設置爲等於框架,因此組件不會滾動。爲了滾動,scroll.contentSize.y> scroll.frame.height –

+0

[scrollView setContentSize:CGSizeMake(xPosition,700)];或者你通過生成的按鈕高度調整高度 –

+0

犯了一個小錯誤,我需要它水平滾動不垂直 –

回答

3

試試這個,

float xPosition = 0; 
    float xPosBut = 0; 

    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, self.view.frame.size.height - 29)]; 
    [scrollView setScrollEnabled:YES]; 

    scrollView.backgroundColor = [UIColor yellowColor]; 

    for (int i = 0; i < 10; i++) { 
     UIButton *testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 10, scrollView.frame.size.width, 50)]; 
     [testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; 
     [testButton setTitle:@"Test" forState:UIControlStateNormal]; 
     [testButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside]; 
     testButton.backgroundColor = [UIColor blueColor]; 
     xPosition += testButton.frame.size.width; 
     NSLog(@"xposbut = %f", xPosBut); 
     NSLog(@"scroll.frame.size.width = %f", scrollView.frame.size.width); 

     xPosition += scrollView.frame.size.width+2; 
     UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 2, scrollView.frame.size.height - 8)]; 
     seperatorView.backgroundColor = [UIColor redColor]; 
     [scrollView addSubview:seperatorView]; 
     xPosition +=scrollView.frame.size.width+ 4; 

     [self.view addSubview:scrollView]; 
     [scrollView addSubview:seperatorView]; 
     [scrollView addSubview:testButton]; 
     [builder addObject:testButton]; 

    } 
    [scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)]; 
    [self.view addSubview:scrollView]; 

設置爲循環條件部分按您的要求。希望這會幫助你。

+0

謝謝你這麼多! –

+0

如果我改變了說'scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,100,self.view.frame.size.width,self.view.frame.size.height - 29)]的代碼行; 'to this:'scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,400,self.frame.size.width,self.frame.size.height - 29)];' 然後它不讓我滾動或按下按鈕? –