即使 contentSize 大于 frame 并且在设置内容大小之前

时间:2023-02-14
本文介绍了即使 contentSize 大于 frame 并且在设置内容大小之前添加了子视图,滚动视图也不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    - (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.

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.

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

As you can see in this picture, I use UINavigationController to wrap a UIViewControllar and set UIImageView as its view. the I created a scrollView and add it on top of the view. then I create another imageView1 and insert it into the scrollView.

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

推荐答案

我找到了答案.看来我需要在 UIImageView 上启用 userInteraction.记得当我使用 UIImageView 作为视图控制器的视图时.因此 UIImageView 的 userInteractionEnabled 属性的默认值为 NO.它根本不接受任何触摸.我们需要像这样启用它

I found out the answer. It seems that I need to enable userInteraction on UIImageView. Remember when I use UIImageView as the view of the view controller. Therefore default value for userInteractionEnabled Property of UIImageView is NO. It ACCEPTS NO touches at all. We need we enable it like this

imageView.userInteractionEnabled = YES;

问题已解决.

这篇关于即使 contentSize 大于 frame 并且在设置内容大小之前添加了子视图,滚动视图也不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:UITableView 状态持久化中的复选框 下一篇:UIButton 的图像使用滚动视图滚动,但它的动作/方

相关文章

最新文章