How to lock the horizontal scrolling of a scrollVi

2019-01-09 05:47发布

i have a scrollView. i want it to scroll only in one direction(VERTICAL). Is there a way in which i can lock the horizontal scrolling...?? ...

10条回答
叛逆
2楼-- · 2019-01-09 06:02

If the UIScrollView's contentSize property has the same as the actual width of the UIScrollView, then no horizontal scrolling will be possible.

Are you wanting to stop horizontal scrolling when horizontal scrolling would be valid? (i.e. the contentSize is wider than the actual UIScrollView width?) If 'yes', why are you wanting to do that?

查看更多
Explosion°爆炸
3楼-- · 2019-01-09 06:03

Same (adapted) answer for you as in:

Disabling vertical scrolling in UIScrollView

right, all answers here are ok...

but if for some strange reason your contentSize should be higher than the UIScrollView in both dimensions, you can disable the horizontal scrolling implementing the UIScrollView protocol method -(void)scrollViewDidScroll:(UIScrollView *)aScrollView

just add this in your UIViewController

float oldX; // here or better in .h interface

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    [aScrollView setContentOffset: CGPointMake(oldX, aScrollView.contentOffset.y)];
    // or if you are sure you wanna it always on left:
    // [aScrollView setContentOffset: CGPointMake(0, aScrollView.contentOffset.y)];
}

it's just the method called when the user scroll your UIScrollView, and doing so you force the content of it to have always the same .x

查看更多
可以哭但决不认输i
4楼-- · 2019-01-09 06:06

oh since iOS7 use this (in initWithNibName):

self.automaticallyAdjustsScrollViewInsets = NO;

and create the 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];
查看更多
萌系小妹纸
5楼-- · 2019-01-09 06:08

See this question, you have to set the scrollable size to the corresponding size of the view so that there is nothing to scroll.

查看更多
地球回转人心会变
6楼-- · 2019-01-09 06:08

Swift

Make contentOffset.x = 0 for disable horizontal scroll

   extension ViewController: UIScrollViewDelegate {
          func scrollViewDidScroll(_ scrollView: UIScrollView) {
            scrollView.contentOffset.x = 0//to lock horizontal scrolling
            //scrollView.contentOffset.y = 0//to lock vertical scrolling
        }
    }

Additionally you can disable scroll indicator as

  scrollView.showsHorizontalScrollIndicator = false//to disable HorizontalScrollIndicator
    scrollView.showsVerticalScrollIndicator = false//to disable VerticalScrollIndicator
查看更多
男人必须洒脱
7楼-- · 2019-01-09 06:09

You can try this,

self.ScrollView.contentSize = CGSizeMake(self.ScrollView.frame.size.width, 1450);

here 1450 is my view height.

查看更多
登录 后发表回答