1

我发生了这个问题,我水平添加了一个滚动视图和一个UITextview滚动视图,但是在模拟器中视图不能很好地滚动。

我的问题是:如何设置 contentsize 以使其滚动?

这是代码:

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

}
4

2 回答 2

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

于 2013-05-31T08:58:47.417 回答
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设置所有子视图的高度。

于 2013-06-05T05:02:28.090 回答