2011-08-19 77 views
4

我知道我在幾分鐘前問過一個問題,但在徵求社區的幫助之前,我想先澄清幾點。我是iOS開發新手,接近完成我的第一個應用程序。唯一站在我這邊的是這個UIScrollView。我只是不明白它,並可以使用你的幫助。用UIScrollView實現垂直滾動的正確方法

這是在具有標籤欄的深入分析應用程序的詳細視圖控制器中。我有大約8個字段(例如電話號碼等)從.plist中繪製。顯然,那些佔用了足夠的空間,我可以使用一些額外的房地產。我猜想它需要是垂直兩個視圖的大小,但我不明白如何將這種空間分配給UIScrollView。我讀過的一些教程說,你甚至不需要在標題中定義它,我懷疑和不明白。此外,我不明白如何簡單地讓應用程序平滑上下滾動。 Apple的例子有不斷的移動循環,在水平圖片之間翻轉。

我懷疑它有多大的不同,但我有一個背景圖片。我將在其上加載視圖。

我的問題很廣泛,所以我不希望你花時間坐下來爲我寫出所有的代碼(我不會要求你)。只需簡要介紹一下快速有用的提示,就可以幫助我理解如何在我的項目環境中加載此視圖。

非常感謝!

回答

6

這很簡單。添加一個UIScrollView到你的視圖。將您的字段等添加到滾動視圖。在viewDidLoad或類似的地方,你需要在你的滾動視圖上設置contentSize。這是可以滾動的「虛擬」區域。這通常會比scrollview的框架更大。在你的情況下,你表示它應該大致是寬度的兩倍。

舉例來說,如果您有相關的內部景觀的滾動視圖,你想確保整個視圖通過滾動可見:

self.scrollView.contentSize = self.contentView.frame.size; 
+0

好吧,如果視圖是滾動視圖的父項,我應該如何向其添加內容(可能是一個愚蠢的問題) –

+0

您,先生,救了我。謝謝! –

1

剛剛成立的UIScrollView的內容大小將所有的控制/按鈕/文本框等,例如當您在UIScrollView中添加10個文本框,然後UIScrollView的內容大小將

lastTextField.frame.origin.y+lastTextField.frame.size.height+20; 20 is for margin. 

就是它讓我知道如果你想知道更多與你的應用程序相關的內容。

+0

我應該如何將所有的內容添加到滾動視圖考慮到它的大小?在界面構建器中有沒有特定的方法來做到這一點? (可能是一個非常愚蠢的問題) –

+0

是的,你也可以在界面構建器中做到這一點。這有點棘手。首先將您的默認UIView類更改爲UIScrollView。然後通過在IB中的UIViewController的屬性中將頂部狀態欄設置爲none來刪除頂部狀態欄。然後拖動儘可能多的放置所有組件,然後再將視圖高度降低到460 aur 416取決於您是否使用導航控制器。然後在視圖中加載設置UIScrollView的內容大小。 –

2
//setting scrollview zoom level 
    self._scrollview.minimumZoomScale=0.5; 

    self._scrollview.maximumZoomScale=6.0; 

    self._scrollview.contentSize=CGSizeMake(320,500); 

你必須在出口.H滾動視圖class和@property @synthesis這個滾動view.then你可以上下滾動,如果你只想垂直滾動,那麼你必須去界面生成器和取消水平滾動。

2

您可以爲滾動視圖設置一些設置,以將滾動限制爲水平或垂直。一些重要的是:

// pseudcode here, start typing "[scrollView " then press escape to get a intelli-sense of all // the things you can set for the scrollview. 

// values could be YES/NO or TRUE/FALSE, I can't remember which one but 
// I think it's YES/NO. Once you start scrolling, the phone will determine 
// which way you're scrolling then lock it to that direction 
[scrollView setDirectionalLockEnabled:YES]; 

// when you slide the view, if enough of the next part of the view is visible, 
// the scrollview will snap or bounce the scrollview to fit this new "page". 
// think of swiping feature to navigate the iPhone home screens 
// to show different "pages" of iphone apps 
[scrollView setPagingEnabled:YES]; 

// as a safe guard, make sure the width of your scrollview fits snuggly with the content 
// it is trying to display. If the width is more than necessary to display your table of 
// data vertically, sometimes the scrollview will cause the 
// horizontal scrolling that you don't want to happen and you get bi-directional scrolling