簡體   English   中英

帶有分頁的ScrollView

[英]ScrollView with Paging

我在ScrollView中添加了3個UIViewController ,就像這樣

[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"view1"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"view2"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"view3"]];

我如何找到可見的?

您只能使用- (void)addChildViewController:(UIViewController *)childController來構建自己的包含視圖控制器(例如,您自己的tabBarController),而不是將子視圖添加到滾動視圖。 有關視圖控制器包含的更多信息,您應該閱讀https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html ,有關簡短摘要,您可以閱讀何時使用addChildViewController和pushViewController

如果要向滾動視圖添加視圖,則只需使用- (void)addSubview:(UIView *)view (例如[self.scrollView addSubview:viewOfViewControllerWithIdentifierView1] )。 還要確保設置了scrollviews contentSize和subviews框架。 但是,如果要在不同的視圖控制器之間滾動到頁面,則建議您看一下UIPageViewController: https : //developer.apple.com/library/ios/documentation/uikit/reference/UIPageViewControllerClassReferenceClassRef/UIPageViewControllerClassReference.html

您可以通過計算滾動視圖的contentoffset進行檢查。 為此,您必須添加Scrollview的Delegate Methode。

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    page_control.currentPage=scrollView.contentOffset.x/320;
}

在上面的代碼中,我有多個寬度為320像素的ChildView控制器,表示完整的iPhone屏幕寬度。 page_control是要為其選擇當前頁面的頁面控制器。

讓我問您是否有任何困惑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM