简体   繁体   English

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

[英]scrollView doesn't work even though contentSize is larger than frame and subview is added before setting content size

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

    }

This is my attempt to create UIScrollView programmatically however I couldn't make the scrollView work even though I set the contentSize after adding the subview into the scrollView . 这是我尝试以编程方式创建UIScrollView尝试,但是即使将子视图添加到scrollView之后设置contentSize ,也无法使scrollView正常工作。

看起来如何

As you can see in this picture, I use UINavigationController to wrap a UIViewControllar and set UIImageView as its view. 如您在此图中看到的,我使用UINavigationController包装UIViewControllar并将UIImageView设置为其视图。 the I created a scrollView and add it on top of the view. 我创建了一个scrollView并将其添加到视图的顶部。 then I create another imageView1 and insert it into the scrollView. 然后创建另一个imageView1并将其插入scrollView中。

please note that the view of the entire view controller is an imageView which is different from the imageView i insert into the scrollView. 请注意,整个视图控制器的视图是一个imageView,它与我插入scrollView中的imageView不同。

You should set your content size using constraints, ton by setting it directly. 您应该使用约束条件设置内容大小,直接设置它的大小。 It does not scroll because you don't have constraints to the left, top, right or bottom of your scroll view content. 它不会滚动,因为您对滚动视图内容的左侧,顶部,右侧或底部没有约束。 See my answer here 在这里查看我的答案

Edit: 编辑:

Try adding this: 尝试添加以下内容:

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

and remove the code where you are setting the content size. 并删除您要设置内容大小的代码。

Edit: 编辑:

Replace your entire code with this: 用以下代码替换整个代码:

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

The only way I have been successful in creating UIScrollViews is to use Autolayout. 我成功创建UIScrollViews的唯一方法是使用自动布局。 This is the simplest and most effective way I know. 这是我知道的最简单,最有效的方法。 For this you will need to remove the 为此,您需要删除

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

Replace: 更换:

NSDictionary *layoutDic = ....

With: 附:

NSDictionary *layoutDic = NSDictionaryOfVariableBindings(scrollView);

Edit: I use your code, and it works, I can scroll, I do in viewDidLoad 编辑:我使用您的代码,并且它可以工作,我可以滚动,我在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];

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

Add the delegate to self like this: 像这样将委托添加到self中:

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

scrollView.delegate=self;

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

I found out the answer. 我找到了答案。 It seems that I need to enable userInteraction on UIImageView. 看来我需要在UIImageView上启用userInteraction。 Remember when I use UIImageView as the view of the view controller. 请记住,当我使用UIImageView作为视图控制器的视图时。 Therefore default value for userInteractionEnabled Property of UIImageView is NO. 因此,UIImageView的userInteractionEnabled属性的默认值为NO。 It ACCEPTS NO touches at all. 它完全不接触。 We need we enable it like this 我们需要像这样启用它

imageView.userInteractionEnabled = YES;

PROBLEM Solved. 问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM