2013-05-31 64 views
1

我發生了這個問題,我向scrollview水平添加了scrollview,而UITextview添加了滾動視圖,但在模擬器中,視圖無法正常滾動。添加子視圖後,UIScrollView無法滾動

我的問題是:如何設置contentize使其滾動?

下面是代碼:

- (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor whiteColor]; 
    self.title = scenery.name; 
    mainScrollView = [[UIScrollView alloc] init]; 

    self.imagePaths = [scenery imagePaths]; 
    imageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 240)]; 
    imageScrollView.directionalLockEnabled = YES; 
    imageScrollView.pagingEnabled = YES; 
    imageScrollView.showsVerticalScrollIndicator = NO; 
    imageScrollView.showsHorizontalScrollIndicator = NO; 
    [self AddImgsToScrollView]; 
    self.currentPage = 0; 
    [NSTimer scheduledTimerWithTimeInterval:1 target: self selector: @selector(handleTimer:) userInfo:nil repeats: YES]; 

    [mainScrollView addSubview:imageScrollView]; 

    //Introduction Text View 
    introView = [[UITextView alloc] init]; 
    introView.text = scenery.introduction; 
    introView.editable = NO; 
    introView.font = [UIFont systemFontOfSize:16.0f]; 
    [introView setScrollEnabled:NO]; 

    CGSize textViewSize = [introView.text sizeWithFont:introView.font constrainedToSize:CGSizeMake(WIDTH-20, FLT_MAX)]; 


    CGRect newFrame = introView.frame; 
    newFrame.size = textViewSize; 
    newFrame.origin = CGPointMake(10,245); 
    introView.frame = newFrame; 
    [mainScrollView addSubview:introView]; 

    CGFloat scrollViewHeight = 0.0f; 
    for (UIView *view in mainScrollView.subviews){ 
     if (scrollViewHeight < view.frame.origin.y + view.frame.size.height) scrollViewHeight = view.frame.origin.y + view.frame.size.height; 
} 
    newFrame = mainScrollView.frame; 
    newFrame.size = CGSizeMake(WIDTH, scrollViewHeight); 
    mainScrollView.frame = newFrame; 
    NSLog(@"%f/%f", scrollViewHeight,textViewSize.height); 

    [mainScrollView setContentSize:CGSizeMake(WIDTH, scrollViewHeight)]; 

    [self.view addSubview:mainScrollView]; 

} 
+0

爲什麼你通過將滾動視圖添加到滾動視圖和另一個滾動視圖來使它變得複雜.....你需要給出明確的規範嗎? 你想如何讓你的視圖看起來像? – alpha

+0

[mainScrollView addSubview:imageScrollView];爲什麼在另一個滾動視圖中添加滾動視圖 – Rushabh

+0

檢查視圖的用戶交互設置爲否。所以這個觸摸事件將被髮送到滾動視圖。 –

回答

0

最後,我得到了答案。

CGRect newFrame = introView.frame; 
newFrame.size = textViewSize; 
newFrame.origin = CGPointMake(10,245); 
introView.frame = newFrame; 
[mainScrollView addSubview:introView]; 

CGFloat scrollViewHeight = introView.contentSize.height + imageScrollView.contentSize.height + 64; 
newFrame = mainScrollView.frame; 
newFrame.size = CGSizeMake(WIDTH, 480); 
newFrame.origin = CGPointMake(0,0); 
mainScrollView.frame = newFrame; 

[mainScrollView setContentSize:CGSizeMake(WIDTH, scrollViewHeight)]; 

1.將mainScrollView框架設置爲屏幕高度。

2.然後使用setContentSize設置所有子視圖的高度。

0

嘗試以下。這是我的工作代碼。

  int scrollWidth=60; 
      for (int i=0;i<array.count;i++) 
      { 
       UIImageView *imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(scrollWidth,8,50,40)]; 
       imageView1.userInteractionEnabled=YES; 
       imageView1.backgroundColor=[array objectAtIndex:i]; 
       [scrollView addSubview:imageView1]; 
       scrollWidth=scrollWidth+80; 
      } 

      [scrollView setContentSize:CGSizeMake(scrollWidth, 30)]; 

還在xib中檢查用戶交互和滾動啓用與否。

+0

在上面提到的代碼對我來說,它總是爲循環添加最後一個視圖。你能告訴我你在viewDidAppear或viewDidLoad中添加了這段代碼嗎? – lifemoveson

+0

不知道您的代碼,無法確定什麼是確切問題。但我想這個問題可能與scrollview內容大小有關,請確保您的設置正確,[scrollView setContentSize:CGSizeMake(scrollWidth,30)]; –

+0

感謝您的答案,但我發現它是這個問題的原因。我沒有初始化每個循環的視圖。 – lifemoveson

相關問題