gpt4 book ai didi

uinavigationcontroller - 在 UINavigationController 中嵌套 UIPageViewController 仅显示一页

转载 作者:行者123 更新时间:2023-12-04 08:43:06 28 4
gpt4 key购买 nike

我已经在这上面花了几个小时,但无法让它工作。希望有人能帮助我。

我有一个 UIPageViewController,当我在应用程序启动时将它添加到我的默认 View 时,它可以完美地工作。这就是我所做的:

//Step 1
//Instantiate the UIPageViewController.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

//Step 2:
//Assign the delegate and datasource as self.
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

//Step 3:
//Set the initial view controllers.
ContentViewController *contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
contentViewController.labelContents = [self.modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];


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

现在我想在我的初始 View 中使用一个导航 Controller ,然后在用户单击按钮时将 UIPageViewController 推送到堆栈上。这也有效,但是当我处于横向模式时,UIPageViewController 只显示一页(水平拉伸(stretch))而不是应该显示的两页。我需要将设备旋转到纵向,然后再回到横向模式,以强制 UIPageViewController 显示两个页面。我将 UIPageViewController 添加到导航 Controller :

[self.navController pushViewController:self.pageViewController animated:NO];

当我调试我的代码时,我发现在没有导航 Controller 的情况下,UIPageViewController 的委托(delegate)方法在启动时被调用。当我将 UIPageViewController 推到导航 Controller 上时,在我旋转设备之前它们不会被调用。

有人知道怎么解决吗?在此先感谢您提供的任何帮助/提示。

最佳答案

this thread 的帮助下解决了这个问题.

关键是在通过选项字典创建 pageViewController 时设置 spineLocation:

    options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
forKey: UIPageViewControllerOptionSpineLocationKey];

而且这个选项应该放在检查中以查看当前界面方向是什么。

在上面提到的线程中,接口(interface)方向由三元运算符检查;我将它放在一个 if 语句中,这样我就可以利用同样的机会将一个额外的 UIViewController 添加到 viewControllers 数组上。

如果界面方向是纵向,创建 pageViewController 时 'options' 仍然为 nil,因此它将继续以只有一个 viewController 的纵向模式创建它。

这是我在 BookViewController 中的整个 viewDidLoad 代码:

- (void)viewDidLoad
{
[super viewDidLoad];

_modelController = [[ModelController alloc] init];

DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0
storyboard:self.storyboard];

NSMutableArray *viewControllers = [NSMutableArray arrayWithObjects:
startingViewController,
nil];
NSDictionary *options;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {

DataViewController *secondViewController = [self.modelController viewControllerAtIndex:1
storyboard:self.storyboard];

[viewControllers addObject:secondViewController];

options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
forKey: UIPageViewControllerOptionSpineLocationKey];
}

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:options];
self.pageViewController.delegate = self;

[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];

self.pageViewController.dataSource = self.modelController;

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

// Set the page view controller's bounds
self.pageViewController.view.frame = self.view.bounds;

[self.pageViewController didMoveToParentViewController:self];

// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

}

关于uinavigationcontroller - 在 UINavigationController 中嵌套 UIPageViewController 仅显示一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12402789/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com