子视图控制器向下移动

时间:2015-04-10 17:26:45

标签: ios objective-c autolayout uipageviewcontroller

我有一个TutorialsViewController,它以编程方式添加了PageViewController:

- (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]; 
}

我使用委托方法将TutorialsContentViewController添加到PageViewController:

- (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;
}

现在,当我在模拟器中运行时,我可以看到一切正常,只是下一个子视图控制器看起来在状态栏上方,并在我滚动时向下移动。

我在父视图控制器中尝试了self.automaticallyAdjustsScrollViewInsets = false,但子视图仍然向下移动。子视图控制器有一个ImageView,其最初的前导,尾随,顶部,底部约束设置为0(这会导致移位)。

当我水平和垂直居中图像并给它固定的高度和宽度时,它工作正常。但我希望图像完全填充子视图控制器。

如何解决这个观点转移问题?请帮忙。

感谢。

2 个答案:

答案 0 :(得分:4)

我似乎找到了它的修复程序,它与我设置的自动布局约束有关。我的子视图控制器包含一个完全填充它的图像视图。所以我对图像视图给出以下约束:

  1. 水平居中容器
  2. 垂直居中容器
  3. 等宽(到容器视图)
  4. Equal Heights(到容器视图)
  5. 这会停止视图移位/闪烁问题。

答案 1 :(得分:3)

我已经在这个答案的帮助下解决了这个问题:  iOS 8 UIPageViewController Applying Constraints After Transitions 通过在PageViewController的子代中替换顶部布局指南视图的所有约束。