即使contentSize大于框架,在设置内容大小之前添加子视图,scrollView也不起作用

- (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent = YES; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"sprites_0001_Rectangle-1.png"] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.translucent = YES; UIImageView *imageView = [[UIImageView alloc]init]; UIImage *image = [UIImage imageNamed:@"sprites_0000s_0008_1st-Page.png"]; imageView.image = image; //imageView.translatesAutoresizingMaskIntoConstraints = NO; imageView.backgroundColor = [UIColor clearColor]; self.view = imageView; if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) { //Write the code to set up view for iPad }else{ UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.backgroundColor = [UIColor clearColor]; scrollView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:scrollView]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[scrollView(==240)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[scrollView(==468)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]]; UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ParentsMock.png"]]; imageView.translatesAutoresizingMaskIntoConstraints = NO; [scrollView addSubview:imageView]; [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[imageView(==1000)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(imageView)]]; [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView(==2000)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(imageView)]]; } 

这是我尝试以编程方式创buildUIScrollView但是我无法使scrollView工作,即使我添加subview到scrollView后设置contentSize

它看起来像什么

正如你所看到的,我使用UINavigationController来包装一个UIViewControllar并将UIImageView设置为它的视图。 我创build了一个scrollView并将其添加到视图顶部。 然后我创build另一个imageView1并将其插入到scrollView。

请注意,整个视图控制器的视图是一个imageView不同于我插入到scrollView的imageView。

您应该使用约束设置您的内容大小,通过直接设置吨。 它不会滚动,因为您没有对滚动视图内容的左侧,顶部,右侧或底部的限制。 在这里看到我的答案

编辑:

尝试添加这个:

  imageView.translatesAutoresizingMaskIntoConstraints = NO; [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"|[imageView(==1000)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(imageView)]]; [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|[imageView(==2000)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(imageView)]]; 

并删除您设置内容大小的代码。

编辑:

用这个replace你的整个代码:

  UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.backgroundColor = [UIColor clearColor]; scrollView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:scrollView]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[scrollView(==240)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[scrollView(==468)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(scrollView)]]; UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ParentsMock.png"]]; imageView.translatesAutoresizingMaskIntoConstraints = NO; [scrollView addSubview:imageView]; [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[imageView(==1000)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(imageView)]]; [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView(==2000)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(imageView)]]; 

我成功创buildUIScrollViews的唯一方法是使用Autolayout。 这是我所知道的最简单,最有效的方法。 为此,您将需要删除

 initWithFrame:CGRectMake(40, 100, 240, 168)]; 

更换:

 NSDictionary *layoutDic = .... 

附:

 NSDictionary *layoutDic = NSDictionaryOfVariableBindings(scrollView); 

编辑:我用你的代码,它的工作原理,我可以滚动,我做viewDidLoad

 UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(40, 100, 240, 168)]; UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"purple_button.png"]]; [scrollView addSubview:imageView]; scrollView.contentSize = CGSizeMake(1000, 2000); scrollView.backgroundColor = [UIColor clearColor]; scrollView.scrollEnabled = YES; scrollView.translatesAutoresizingMaskIntoConstraints = NO; NSDictionary *layoutDic = NSDictionaryOfVariableBindings(scrollView); [self.view addSubview:scrollView]; NSArray *scrollViewConstraintWidth = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[scrollView(==240)]" options:0 metrics:nil views:layoutDic]; NSArray *scrollViewConstraintHeight = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[scrollView(==468)]" options:0 metrics:nil views:layoutDic]; [self.view addConstraints:scrollViewConstraintHeight]; [self.view addConstraints:scrollViewConstraintWidth]; 

在“显示文件检查器”下的属性中取消选中“使用自动布局”

像这样添加委托给自己:

  UIScrollView *scrollView = [[UIScrollView alloc]init]; scrollView.delegate=self; UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ParentsMock.png"]]; [scrollView addSubview:imageView]; 

我find了答案。 看来我需要在UIImageView上启用userInteraction。 请记住,当我使用UIImageView作为视图控制器的视图。 因此,UIImageView的userInteractionEnabled属性的默认值为NO。 它根本没有接触。 我们需要我们这样启用它

 imageView.userInteractionEnabled = YES; 

问题解决了。