問(wèn)題描述
- (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)]];
}
這是我以編程方式創(chuàng)建 UIScrollView
的嘗試,但是即使在添加子視圖進(jìn)入 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 來(lái)包裝 UIViewControllar 并將 UIImageView 設(shè)置為其視圖.我創(chuàng)建了一個(gè)滾動(dòng)視圖并將其添加到視圖頂部.然后我創(chuàng)建另一個(gè) 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.
請(qǐng)注意,整個(gè)視圖控制器的視圖是一個(gè)imageView,與我插入到scrollView中的imageView不同.
推薦答案
我找到了答案.看來(lái)我需要在 UIImageView 上啟用 userInteraction.記得當(dāng)我使用 UIImageView 作為視圖控制器的視圖時(shí).因此 UIImageView 的 userInteractionEnabled 屬性的默認(rèn)值為 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;
問(wèn)題已解決.
這篇關(guān)于即使 contentSize 大于 frame 并且在設(shè)置內(nèi)容大小之前添加了子視圖,滾動(dòng)視圖也不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!