UIScrollView does not scroll

I have a UIScrollView that doesn't scroll at all. If I turn on bouncing, I can scroll far enough to the sides to see that there is content outside the view, but it drops back to the origin when I release. I have paging enabled, but I get the same behavior if I disable it. I have disabled auto-layout. In IB, scrollView is in the MainViewController correctly associated with the IBOutlet, as shown below.

@interface MainViewController () @property (strong, nonatomic) IBOutlet UIScrollView *scrollView; @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; for (int i = 0; i < 3; i++) { CGRect frame; frame.origin.x = self.scrollView.frame.size.width * i; frame.origin.y = 0; frame.size = self.scrollView.frame.size; // I might be doing this wrong, but it returns a PosterView object just like I want it to. NSArray *view = [[NSBundle mainBundle] loadNibNamed:@"posterView" owner:self options:nil]; [[view objectAtIndex:0] setFrame:frame]; [self.scrollView addSubview:[view objectAtIndex:0]]; } self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 3, self.scrollView.frame.size.height); } 

If I use this method, it scrolls correctly.

 -(void)scrollToPage:(NSInteger)page{ float pageWidth = [self.scrollView frame].size.width; [self.scrollView setContentOffset:CGPointMake(page*pageWidth,0) animated:YES]; } 

I tried to implement UIScrollViewDelegate and override scrollViewDidScroll , but it is never called (unless I have permission to roll back, as I mentioned earlier).

+4
source share
2 answers

UIScrollView only scrolls if the contentSize larger than its frame size. If it does not scroll, it may mean that its contentSize not large enough. So check its contentSize after installing it.

If the contentSize is what you expect, then check if scrollView is scrollEnabled or not. Sometimes the inclusion of a rebound gives the impression that scrolling works. In addition, if scrolling is not enabled, setting contentOffset also works in this case, because then it moves the content according to the provided size as an argument.

+8
source

You must set contentSize inside viewDidLayoutSubviews . It is late enough in the vision life cycle so that it is not reset in any other way and does not require you to check if the view scrolls for the first time.

+3
source

Source: https://habr.com/ru/post/1489772/


All Articles