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:10
self.scrollview.contentSize = CGSizeMake(1, self.scrollview.frame.size.height * number_of_items);

should help. I just locked my scroll view horizontally, by doing the similar technique.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-09 06:14

The scrollView is only scrollable horizontally if you set the contentSize width bigger than the scrollView frame. If you set the contentSize width to the same as the scrollview it won't scroll. For example

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,100,100)] [scrollView setContentSize:CGSizeMake(100, 300)]

查看更多
Lonely孤独者°
4楼-- · 2019-01-09 06:18

The safest and most successful method I've found to constrain the movement of a scroll view is to subclass UIScrollView and override setContentOffset:animated: and setContentOffset: methods (code below).

The advantage of overriding these methods is that it directly alters the requested contentOffset before any of the UIKit code starts to act on it, avoiding any of the side effects that can occur when modifying the contentOffset in scrollViewDidScroll: or other UIScrollViewDelegate methods.

It takes just a few minutes to create the new subclass, and it works like a charm.

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
    // restrict movement to vertical only
    CGPoint newOffset = CGPointMake(0, contentOffset.y);
    [super setContentOffset:newOffset animated:animated];
}

- (void)setContentOffset:(CGPoint)contentOffset {
    // restrict movement to vertical only
    CGPoint newOffset = CGPointMake(0, contentOffset.y);
    [super setContentOffset:newOffset];    
}
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-09 06:19

In my case the contentSize was higher than the UIScrollView for x and y. For me meronixe's answer worked out quite well, but was for my needs not quite complete. Unfortunately the horizontal scrollbar was still visible.

This I could change with the following code in

- (void)webViewDidFinishLoad:(UIWebView *)webView

[self.webView.scrollView setShowsHorizontalScrollIndicator:NO];
查看更多
登录 后发表回答