2011-05-22 76 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