1

I have a problem with my UIScrollView and I hope some of you can help me in some way.

I explain you ...

At the center of my ViewController I have a ScrollView showing the contents of 3 external UIViewController. Through classic for loops I am able to properly display the contents and so far I have not found any problems ...

The problem arises when I perform scrolling between the view controller (horizontally) or, the content of each view controller moves, it does not remain as it was initially set. Now I can not figure out if the problem depends ScrollView or by content, but I think it is a problem of content viewcontroller because I used the autolayout to set constraints ...

At this point I think it's a problem that arises in the ScrollView when I make scrolling to show the next viewcontroller.

Below I insert the screen shoots to show my situation .. as you can see the contents are moved to the right and do not remain in the X position zero.

Can you help me figure out where I'm wrong? what code and 'wrong ..

ScreenShoot

enter image description here

enter image description here

enter image description here

code used

    -(void)initializeViewControllerTour {
    /* Inserisce in una NSMutableArray i ViewController inizializzati da utilizzare*/
    _a_TourVC = [[UPATour alloc] init];
    _a_TourVC = [self.storyboard instantiateViewControllerWithIdentifier:@"A_Tour_App"];

    _b_TourVC = [[UPBTour alloc] init];
    _b_TourVC = [self.storyboard instantiateViewControllerWithIdentifier:@"B_Tour_App"];

    _c_TourVC = [[UPCTour alloc] init];
    _c_TourVC = [self.storyboard instantiateViewControllerWithIdentifier:@"C_Tour_App"];

    _containerArrayVC = [[NSMutableArray alloc] init];
    [_containerArrayVC addObject:_a_TourVC];
    [_containerArrayVC addObject:_b_TourVC];
    [_containerArrayVC addObject:_c_TourVC];

    NSInteger index = 0;

    /* Imposta i valori per la ScrollView */
    CGSize scrollViewSize = CGSizeMake([_containerArrayVC count] * _scrollView.frame.size.width, _scrollView.frame.size.height);

    _scrollView.contentSize = scrollViewSize;
    _scrollView.scrollEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;

    /* Imposta il PageControl */
    _pageControl.userInteractionEnabled = NO;
    _pageControl.numberOfPages = [_containerArrayVC count];
    _pageControl.currentPage = index;

    /* Crea il ciclo For per i ViewController basandosi sulla MutableArray */
    for (_currentViewController in _containerArrayVC) {
        [self addChildViewController:_currentViewController];
        CGFloat originx = (index) * _scrollView.frame.size.width;
        _currentViewController.view.frame = CGRectMake(originx, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
        [_scrollView addSubview:_currentViewController.view];
        [_currentViewController didMoveToParentViewController:self];
        index ++;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageW = _scrollView.frame.size.width;
    CGFloat segmentPage = _scrollView.contentOffset.x / pageW;
    NSInteger page = lround(segmentPage);
    _pageControl.currentPage = page;

}
kAiN
  • 2,559
  • 1
  • 26
  • 54
  • 1
    According to your requirement you can take collection view and use horizontal paging. It saves you mind and time alot. – dahiya_boy Feb 09 '17 at 13:29
  • @the_dahiya_boy Can you give me an example to understand better ... I did not understand well understood how I should do – kAiN Feb 09 '17 at 13:36
  • due to lack of time I unable to explain you, but go with this [link](http://stackoverflow.com/questions/29658328/uicollectionview-horizontal-paging-not-centered) it helps you alot. Later I will explain you if you have doubt. – dahiya_boy Feb 09 '17 at 13:43
  • @the_dahiya_boy I realized I advice to use a collection view to achieve the same effect. My problem is that I'll need to manually insert animations in code for other elements in addition to those that you see in the screenshots, that's why I thought to viewController .. If you intend to use a collection view I would not be a bit limited in this? – kAiN Feb 09 '17 at 13:48
  • Even you can do same in one UIViewController. `UIView.animation` for this – dahiya_boy Feb 09 '17 at 13:52
  • nothing ideas???? – kAiN Feb 10 '17 at 20:38

0 Answers0