I have a horizontal scroll view that is a subview of a vertical scroll view. It looks like the vertical scroll view is intercepting all user interactions. I want to be able to scroll horizontally the horizontal scroll view. How would I do this?
Here is a short snippet of my code:
UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 100)];
scrollView2.contentSize = CGSizeMake(self.view.bounds.size.width + 400, 100);
_scrollView.contentSize = CGSizeMake(self.view.width, 400);
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.height - 108)];
[self.view addSubview:_scrollView];
[_scrollView addSubview:scrollView2];
You can subclass the parent scrollview and override touchesShouldCancelInContentView:
to return NO whenever the view is child scroll view. ie;
MyScrollView.h
@interface MyScrollView: UIScrollView
@end
and in MyScrollView.m
@implementation MyScrollView
-(BOOL)touchesShouldCancelInContentView:(UIView *)touchedView{
if(touchedView == _scrollView2){ //Get the reference of that child scroll view here
return NO;
}else{
[super touchesShouldCancelInContentView:touchedView];
}
}
Then your posted code should look like:
UIScrollView *scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 100)];
scrollView2.contentSize = CGSizeMake(self.view.bounds.size.width + 400, 100);
_myScrollView = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.height - 108)];
_myScrollView.contentSize = CGSizeMake(self.view.width, 400);
[_myScrollView addSubview:scrollView2];
[self.view addSubview:_myScrollView];
Unable to comment. Hence answering here.
I faced this problem earlier. I do not remember the exact solution. But I think the problem was that you are trying to add the second view controller after the second. I think you should try the reverse.
[_scrollView addSubview:scrollView2];
[self.view addSubview:_scrollView];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.