How to disable horizontal scrolling of UIScrollView?

前端 未结 14 1661
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 08:17

I have a UIView like iPhone\'s Springboard. I have created it using a UIScrollView and UIButtons. I want to disable horizontal scrolli

相关标签:
14条回答
  • 2020-12-02 09:01

    since iOS7 use

    self.automaticallyAdjustsScrollViewInsets = NO;
    

    //and create you page scroller with 3 pages

        self.pageView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [self.pageView setContentSize:CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)];
        [self.pageView setShowsVerticalScrollIndicator:NO];
        [self.pageView setPagingEnabled:YES];
        [self.view addSubview:self.pageView];
    
    0 讨论(0)
  • 2020-12-02 09:05

    UPDATED: (After @EranMarom pointed out on his comment)

    You can stop horizontal scrolling or vertical scrolling in the ScrollViewDelegate Method. Here it is how,

    Stops Horizontal Scrolling:

    If you want to scroll horizontally, then you need to increase the contentOffset.x. Preventing that stops the scrollview scroll in horizontal direction.

    - (void)scrollViewDidScroll:(UIScrollView *)sender {
        sender.contentOffset.x = 0.0
    }
    

    Stops Vertical Scrolling:

    If you want to scroll vertically, then you need to increase the contentOffset.y. Preventing that stops the scrollview scroll in vertical direction.

    - (void)scrollViewDidScroll:(UIScrollView *)sender {
        sender.contentOffset.y = 0.0
    }
    

    Above code prevents the changes in x and y of a scrollview contentOffset and it leads to stop the scrolling in scrollViewDidScroll: method.

    0 讨论(0)
提交回复
热议问题