2011-05-22 71 views
6

嘿人, 我有一些数据,并希望在导航堆栈内的新视图中显示。iphone:uiscrollview不滚动?但内容是设置..?

我用XIB文件创建了一些新的viewcontroller类。然后,我编辑了XIB文件,并放置了以下的层次:

1 files owner 
2 first responder 
3 scrollview 
3.1 view 
3.1.1 label1 
3.1.2 label2 
3.1.3 label3 
3.1.4 image4 

我安排了标签和图像。标签的内容可能会有所不同,并且它们应该动态调整大小。我也在viewcontroller类中做了IBOutlets,并正确连接了所有东西。文件的所有者的观点委托设置,查看..

然后我加载视图 - 控制和我在“viewDidLoad中”做 - 方法如下:

- (void)viewDidLoad { 
[super viewDidLoad]; 

self.currentPageURL.text = [self.offerItem objectForKey: @"page-url"]; 
self.currentPrice.text = [self.offerItem objectForKey: @"price"]; 
self.currentRank.text = [self.offerItem objectForKey: @"rank"]; 
self.currentName.text = [self.offerItem objectForKey: @"name"]; 
self.currentProducerName.text = [self.offerItem objectForKey: @"producer-name"]; 

self.currentDescription.text = [self.offerItem objectForKey: @"description"]; 

self.imageViewForImage.image = [helperClass resizeImage:[self.offerItem objectForKey: @"picture"] forSize:CGSizeMake(280.0, 280.0) ]; 

[theScrollview setScrollEnabled:YES]; 
[theScrollview setContentSize:CGSizeMake(320, 1200)]; 
[theScrollview flashScrollIndicators]; 

[theScrollview addSubview:theView]; 

}

当我启动应用程序在我的模拟器中,一切都显示,但如果我尝试滚动,没有任何反应。

我该怎么做错?

+0

是什么的视图'帧大小3.1' ? – 2011-05-22 16:11:31

+0

我在IB中设置大小为320x1200 ..滚动视图也在IB中设置为320x1200 .. – dac 2011-05-22 16:24:32

回答

4

立即将theScrollView的框架更改为320x480,您应该看到滚动。

+1

我这样做了 - 但问题保持不变.. 滚动视图在320x480的IB中,下面的视图在320x1200 .. – dac 2011-05-22 16:34:55

+0

所以基本上你能看到部分内容但不能滚动?那么'userInteractionEnabled'属性呢?它是否设置为YES? – 2011-05-22 16:39:40

+0

是的,它是在IB中设置的,我额外添加了[theScrollview setUserInteractionEnabled:YES];以编程方式在上面的ViewDidLoad方法 - 没有反应,没有滚动...是我上面张贴的堆栈正确吗? scrollview有一个视图作为孩子? scrollview具有iphone屏幕的维度并查看整个内容的维度? – dac 2011-05-22 16:45:19

17

Autolayout在视图加载之后设置一些约束,因此,选择代码来设置viewDidLoad中的内容大小并放入viewDidAppear中,并且您的代码不会被重写。

这对我有效。

-(void)viewDidAppear:(BOOL)animated{ 
    [self.scrollView setContentSize:CGSizeMake(320, 1000)]; 
} 

对不起,我的英文很差。

+1

为我工作!而不是使用CGSizeMake,你也可以根据scrollview中的内容计算宽度/高度。 – wspruijt 2014-11-27 15:00:05

8

确保在IB文档属性中使用“使用Autolayout”为未选中。我有完全相同的问题,设置正确的contentSize和所有,但启用此功能,滚动始终禁用。

Use Autolayout needs to be OFF

UPDATE要使用自动布局,明确地设置滚动视图的在viewDidAppear方法内容的宽度和高度,这样的:

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    // set the scrollview to the specified size 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    [scrollView setContentSize:CGSizeMake(screenRect.size.width, 1100)]; 

    [scrollView setBounds:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)]; 

    [scrollView setScrollIndicatorInsets:UIEdgeInsetsFromString(@"{0,0,0,-1.0}")]; 

} 
+0

为什么这样工作?你应该在viewDidAppear中声明内容! – david 2014-09-08 14:32:50