Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child View Controller shifts down

I have a TutorialsViewController that has a PageViewController added programmatically as:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Create page view controller
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TutorialsPageViewController"];
    self.pageViewController.dataSource = self;

    TutorialsContentViewController *startingViewController = [self viewControllerAtIndex:0];

    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self]; 
}

And I add TutorialsContentViewController to the PageViewController using the delegate methods:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = ((TutorialsContentViewController*) viewController).pageIndex;

    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }

    index--;
    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = ((TutorialsContentViewController*) viewController).pageIndex;

    if (index == NSNotFound) {
        return nil;
    }

    index++;
    if (index == [self.tutorialsArray count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}

- (TutorialsContentViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (([self.tutorialsArray count] == 0) || (index >= [self.tutorialsArray count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    TutorialsContentViewController *tutorialsContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"TutorialsContentViewController"];
    tutorialsContentViewController.imageName = [self.tutorialsArray objectAtIndex:index];
    tutorialsContentViewController.pageIndex = index;
    return tutorialsContentViewController;
}

Now when i run this in simulator, i can see everything working fine, just that the next child view controller appears to be above the status bar and it shifts down as I scroll.

I have tried self.automaticallyAdjustsScrollViewInsets = false in the Parent View Controller but the child view still shifts down. The child view controller has an ImageView that has leading, trailing, top, bottom constraints set to 0 initially (this causes the shift).

When i centre the image horizontally and vertically and give it fixed height and width, it works fine though. But i want the image to completely fill the child view controller.

How to solve this view shifting problem? Please help.

Thanks.

like image 256
Kiran Thapa Avatar asked Apr 10 '15 17:04

Kiran Thapa


1 Answers

I seem to have found the fix for it, it was related to the auto layout constraints i had set up. My child view controller contains an image view that completely fills it. So i give following constraints to the image view:

  1. Center Horizontally In Container
  2. Center Vertically In Container
  3. Equal Widths (to the container view)
  4. Equal Heights (to the container view)

This stops the view shifting/flickering problem.

like image 132
Kiran Thapa Avatar answered Sep 23 '22 10:09

Kiran Thapa