即使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)]];
}
这是我尝试创建 UIScrollView
以编程方式然后我无法使 scrollView
工作,即使我设置 contentSize
将子视图添加到 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设置为其视图。我创建了一个scrollView并将其添加到视图顶部。然后我创建另一个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 i不同的imageView插入scrollView。
推荐答案
我找到了答案。看来我需要在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并且在设置内容大小之前添加了子视图,scrollView也不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!