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

时间:2014-05-30 04:35:05

标签: ios objective-c uiscrollview

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

    }

这是我尝试以编程方式创建UIScrollView,但即使我在将scrollView添加到子视图后设置contentSize,我也无法使scrollView正常工作。

How it looks like

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

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

6 个答案:

答案 0 :(得分:1)

您应该通过直接设置约束来设置内容大小。它不会滚动,因为您不会对滚动视图内容的左侧,顶部,右侧或底部有约束。请参阅我的回答here

修改

尝试添加此内容:

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

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

修改

用以下代码替换整个代码:

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

替换:

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

答案 3 :(得分:0)

显示文件检查器下的属性中的

取消选中“使用自动布局”

答案 4 :(得分:0)

将委托添加到self中,如下所示:

 UIScrollView *scrollView = [[UIScrollView alloc]init];

scrollView.delegate=self;

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

答案 5 :(得分:0)

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

imageView.userInteractionEnabled = YES;

问题已解决。