UIScrollView: paging horizontally, scrolling vertically?

后端 未结 20 1459
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 09:09

How can I force a UIScrollView in which paging and scrolling are on to only move vertically or horizontally at a given moment?

My understanding is that

20条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 09:39

    What I did was create a paging UIScrollView with a frame the size of the view screen and set the content size to the width needed for all of my content and a height of 1 (thanks Tonetel). Then I created menu UIScrollView pages with frames set to each page, the size of the view screen, and set the content size of each to the width of the screen and the necessary height for each one. Then I simply added each of the menu pages to the paging view. Make sure paging is enabled on the paging scroll view but disabled on the menu views (should be disabled by default) and you should be good to go.

    The paging scroll view now scrolls horizontally, but not vertically because of the content height. Each page scrolls vertically but not horizontally because of its content size constraints as well.

    Here's the code if that explanation left something to be desired:

    UIScrollView *newPagingScrollView =  [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
    [newPagingScrollView setPagingEnabled:YES];
    [newPagingScrollView setShowsVerticalScrollIndicator:NO];
    [newPagingScrollView setShowsHorizontalScrollIndicator:NO];
    [newPagingScrollView setDelegate:self];
    [newPagingScrollView setContentSize:CGSizeMake(self.view.bounds.size.width * NumberOfDetailPages, 1)];
    [self.view addSubview:newPagingScrollView];
    
    float pageX = 0;
    
    for (int i = 0; i < NumberOfDetailPages; i++)
    {               
        CGRect pageFrame = (CGRect) 
        {
            .origin = CGPointMake(pageX, pagingScrollView.bounds.origin.y), 
            .size = pagingScrollView.bounds.size
        };
        UIScrollView *newPage = [self createNewPageFromIndex:i ToPageFrame:pageFrame]; // newPage.contentSize custom set in here        
        [pagingScrollView addSubview:newPage];
    
        pageX += pageFrame.size.width;  
    }           
    

提交回复
热议问题