0

在我的应用程序中,我有一个 CameraAppViewController,我从它导航到另一个视图控制器 OverlayViewController,然后我从它导航到另一个视图控制器 ScrollerViewController。我想在 scrollerviewcontroller 上有一个 uiscrollview,但它不滚动。但是,当我将 uiscrollview 的相同代码放在第一个 CameraAppViewController 时,它会滚动。与视图的框架相比,我尝试了所有改变滚动视图内容大小的技巧,但没有任何效果。我认为代码是正确的,因为它适用于一个视图控制器。我什至试图把它放到另一个项目中,它对第一个和第二个视图控制器都有效。我将不胜感激任何帮助。

我使用的代码如下:

UIImage *image1=[UIImage imageNamed:@"logo_idv0.jpg"];
UIImage *image2=[UIImage imageNamed:@"logo_idv1.jpg"];
UIImage *image3=[UIImage imageNamed:@"logo_idv2.jpg"];
NSMutableArray *allImages=[[NSMutableArray alloc] init];

[allImages addObject:image1];
[allImages addObject:image2];
[allImages addObject:image3];


self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];


scroll.pagingEnabled = YES;

scroll.scrollEnabled = YES;
scroll.clipsToBounds = YES;

NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat yOrigin = i * self.view.frame.size.width;


    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];

    UIImageView *imageView=[[UIImageView alloc] initWithImage:[allImages objectAtIndex:i]];

    [awesomeView addSubview:imageView];

    awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [scroll addSubview:awesomeView];

}

scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height+10);

[self.view addSubview:scroll];
4

1 回答 1

0

首先在 .h 中执行此操作:

@interface SecondViewController : UIViewController <UIScrollViewDelegate>

然后在 .m 中执行此操作:

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];

self.scroll.delegate = self;
于 2012-06-29T14:55:52.740 回答