在UIScrollView下使用单个子视图时,UIViewController视图的属性框架不起作用

时间:2013-04-05 04:21:33

标签: ios uiview uiscrollview

在使用UIScrollView的缩放功能之前,一切都很好。 代码如下:

 UIScrollView* scrollView = [[UIScrollView alloc]init];
   for(int i=0; i < foodIntroCount; ++i) {
        UIImage* image = [UIImage imageNamed:@"7_1.jpg"];
        UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
        imgView.frame = CGRectMake(0, height*i, width, height);
        [scrollView addSubView:imgView];
   }

   scrollView.contentSize =  CGSizeMake(width, height*(foodIntroCount+1)) ;
   scrollView.pagingEnabled = YES; 
   scrollView.showsVerticalScrollIndicator = NO;
   scrollView.showsHorizontalScrollIndicator = NO;
   scrollView.bounces = NO;
   scrollView.delegate = self;


   _foodWineViewController = [[OHFoodWineViewController alloc]initWithCategoryData:_foodData];
   UIView* wineView = _foodWineViewController.view;
   wineView.frame = CGRectMake(0, height*(foodIntroCount), width, height);
   [scrollView addSubview:wineView];

添加缩放功能后,代码如下所示:

UIScrollView* scrollView = [[UIScrollView alloc]init];
UIView* zoomView = [[UIView alloc]init];
   for(int i=0; i < foodIntroCount; ++i) {
        UIImage* image = [UIImage imageNamed:@"7_1.jpg"];
        UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
        imgView.frame = CGRectMake(0, height*i, width, height);
        [zoomView addSubView:imgView];
   }

   scrollView.contentSize =  CGSizeMake(width, height*(foodIntroCount+1)) ;
   scrollView.pagingEnabled = YES; 
   scrollView.showsVerticalScrollIndicator = NO;
   scrollView.showsHorizontalScrollIndicator = NO;
   scrollView.bounces = NO;
   scrollView.delegate = self;
   scrollView.maximumZoomScale = 2.0;


   _foodWineViewController = [[OHFoodWineViewController alloc]initWithCategoryData:_foodData];
   UIView* wineView = _foodWineViewController.view;
   wineView.frame = CGRectMake(0, height*(foodIntroCount), width, height);
   [zoomView addSubView:wineView];
   [scrollView addSubview:zoomView];

问题是: wineView显示在第一页上,而不是在foodIntroCount + 1页面上。

1 个答案:

答案 0 :(得分:0)

如果要垂直添加子视图,则代码应考虑先前的 y位置高度。所以你的代码应该如下所示

imgView.frame = CGRectMake(0, height*i+y, width, height);

虽然代码没有经过测试,试试这个..