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...?? ...
问题:
回答1:
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
回答2:
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:
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];
}
回答4:
See this question, you have to set the scrollable size to the corresponding size of the view so that there is nothing to scroll.
回答5:
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?
回答6:
You can try this,
self.ScrollView.contentSize = CGSizeMake(self.ScrollView.frame.size.width, 1450);
here 1450 is my view height.
回答7:
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)]
回答8:
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];
回答9:
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];
回答10:
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