添加子视图后,UIScrollView无法滚动



我发生了这个问题,我添加了横向scrollview和UITextview到scrollview,但在模拟器中视图不能很好地滚动。

我的问题是:如何设置内容大小使其滚动?

代码如下:

- (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];
}

尝试以下操作。这是我的工作代码。

            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中用户交互和滚动是否启用。

我终于得到了答案。

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来设置所有子视图的高度。

最新更新