xcode 使用 UINavigationController 在 UIPageViewController 中按下的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18202475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Content pushed down in a UIPageViewController with UINavigationController
提问by djibouti33
UPDATE 2
更新 2
I've been running and testing my app in the iOS Simulator using a 4-inch device. If I run using a 3.5-inch device the label doesn't jump. In my .xib, under Simulated Metrics, I have it set as Retina 4-inch Full Screen. Any idea why I'm only seeing this problem on a 4-inch device?
我一直在使用 4 英寸设备在 iOS 模拟器中运行和测试我的应用程序。如果我使用 3.5 英寸设备跑步,则标签不会跳动。在我的 .xib 中,在模拟指标下,我将其设置为 Retina 4 英寸全屏。知道为什么我只在 4 英寸设备上看到这个问题吗?
UPDATE 1
更新 1
In IB, if I choose "Navigation Bar" in Simulated Metrics, my label still jumps. The only way I can get my label to render correctly on the first screen is to not set a navigation controller as my window's root view controller.
在IB中,如果我在Simulated Metrics中选择“Navigation Bar”,我的标签仍然会跳。让我的标签在第一个屏幕上正确呈现的唯一方法是不将导航控制器设置为我的窗口的根视图控制器。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My window's rootViewController is being set to a UINavigationController whose rootViewController has a UIPageViewController embedded.
我的窗口的 rootViewController 被设置为 UINavigationController,其 rootViewController 嵌入了 UIPageViewController。
When my app loads, the initial view is presented with it's content pushed down a bit, roughly the same size as a navigation bar. When I scroll the pageViewController, the content jumps up to where it was placed in the nib, and all other viewControllers loaded by the pageViewController are fine.
当我的应用程序加载时,初始视图会显示其内容向下推一点,大小与导航栏大致相同。当我滚动 pageViewController 时,内容会跳到它在笔尖中的位置,并且 pageViewController 加载的所有其他 viewController 都很好。
In my appDelegate:
在我的 appDelegate 中:
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ContainerViewController new]];
In ContainerViewController:
在 ContainerViewController 中:
- (void)viewDidLoad {
[super viewDidLoad];
self.pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
self.pvc.dataSource = self;
self.pvc.delegate = self;
DetailViewController *detail = [DetailViewController new];
[self.pvc setViewControllers:@[detail]
direction:UIPageViewControllerNavigationDirectionForward
animated:false
completion:nil];
[self addChildViewController:self.pvc];
[self.view addSubview:self.pvc.view];
[self.pvc didMoveToParentViewController:self];
}
回答by djibouti33
So I'm adding another answer after further development and I finally think I figured out what's going on. Seems as though in iOS7, UIPageViewController has its own UIScrollView. Because of this, you have to set automaticallyAdjustsScrollViewInsets
to false. Here's my viewDidLoad
now:
所以我在进一步开发后添加了另一个答案,我终于想我知道发生了什么。好像在 iOS7 中,UIPageViewController 有自己的 UIScrollView。因此,您必须设置automaticallyAdjustsScrollViewInsets
为 false。这是我的viewDidLoad
现在:
- (void)viewDidLoad
{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = false;
DetailViewController *detail = [[DetailViewController alloc] init];
[self setViewControllers:@[detail]
direction:UIPageViewControllerNavigationDirectionForward
animated:false
completion:nil];
}
No need to put anything in viewWillLayoutSubviews
(as one of my previous answers suggested).
无需放入任何东西viewWillLayoutSubviews
(正如我之前的答案之一所建议的那样)。
回答by John Gibb
This is definitely being caused by automaticallyAdjustsScrollViewInsets
, as other posters (including @djibouti33). However, this property is strange in two ways:
这肯定是由automaticallyAdjustsScrollViewInsets
, 和其他海报(包括@djibouti33)引起的。然而,这个属性在两个方面很奇怪:
- It must be set on a
UINavigationController
. If you set it on a child controller that's managed by aUINavigationController
, it won't have any effect. 1 - It only applies when a scroll view is at index zero in a controller's subviews. 2
- 它必须设置在
UINavigationController
. 如果将其设置在由 a 管理的子控制器上UINavigationController
,则不会产生任何效果。1 - 它仅适用于滚动视图在控制器子视图中索引为零的情况。2
These two caveats should explain the intermittent problems experienced by others in the thread.
这两个警告应该可以解释线程中其他人遇到的间歇性问题。
TLDR:A workaround that I went with is adding a dummy view to the UIPageViewController
at index zero, to avoid the setting applying to the scrollView within the page controller, like this:
TLDR:我采用的一种解决方法是UIPageViewController
在索引零处添加一个虚拟视图,以避免将设置应用于页面控制器中的 scrollView,如下所示:
pageViewController.view.insertSubview(UIView(), atIndex: 0) // swift
[pageViewController.view insertSubview: [UIView new] atIndex: 0]; // obj-c
Better would be to set the contentInset
on the scroll view yourself, but unfortunately the UIPageViewController
doesn't expose the scroll view.
最好contentInset
自己在滚动视图上设置,但不幸的UIPageViewController
是 不公开滚动视图。
回答by Bart?omiej Semańczyk
回答by djibouti33
My original answer solved the problem for the time being, but after a while the same problem came back to bite me.
我原来的回答暂时解决了这个问题,但过了一会儿,同样的问题又回来了。
Using the following viewController hierarchy:
使用以下 viewController 层次结构:
-- UINavigationController
-- MyPageViewController
-- MyDetailViewController
Here's what I did to solve it:
这是我为解决它所做的:
In MyPageViewController.m
在 MyPageViewController.m 中
@interface MyPageViewController () <delegates>
@property (strong) MyDetailViewController *initialViewController;
@end
- (void)viewDidLoad
{
...
// set this once, because we're going to use it in viewWillLayoutSubviews,
// which gets called multiple times
self.initialViewController = [MyDetailViewController new];
}
// the problem seemed to stem from the fact that a pageViewController couldn't
// properly lay out it's child view controller initially if it contained a
// scroll view. by the time we're in layoutSubviews, pageViewController seems to
// have gotten it's bearings and everything is laid out just fine.
- (void)viewWillLayoutSubviews
{
[self setViewControllers:@[self.initialViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:false
completion:nil];
}
回答by Danny
I had a similar problem but none of the solutions here worked. My problem was that whenever I would scroll to the next page, the content would jump down, ending in the correct position, but starting 20 pixels too high (clearly something to do with the status bar). My container VC was not a nav VC. After pulling my hair out for a while, the solution that ended up working for me was just to make sure that none of the constraints in my content VC were connected to the top layout guide. This may or may not be feasible in your case, but in mine it was, and it was the only thing that solved the content jump. Also very curiously, this problem only manifested when the transition style was set to scroll. Just changing it to page curl made the issue disappear. But I needed scroll. Hope this helps someone else.
我遇到了类似的问题,但这里的解决方案都没有奏效。我的问题是,每当我滚动到下一页时,内容都会向下跳,以正确的位置结束,但起始位置太高 20 像素(显然与状态栏有关)。我的容器 VC 不是导航 VC。拉了我的头发一段时间后,最终对我有用的解决方案只是确保我的内容 VC 中的任何约束都没有连接到顶部布局指南。这在您的情况下可能可行,也可能不可行,但在我的情况下可行,而且这是解决内容跳转的唯一方法。同样很奇怪的是,这个问题只有在过渡样式设置为滚动时才会出现。只需将其更改为页面卷曲即可使问题消失。但我需要滚动。希望这对其他人有帮助。
回答by howa
I have the same problem. I solve it by putting setViewControllers for the first page in UIPageViewController's viewDidLoad instead of setting it when I make a instance of UIPageViewController. Also, I need to set automaticallyAdjustsScrollViewInsets to NO.
我也有同样的问题。我通过将第一页的 setViewControllers 放在 UIPageViewController 的 viewDidLoad 中来解决它,而不是在我创建 UIPageViewController 的实例时设置它。另外,我需要将 automaticAdjustsScrollViewInsets 设置为 NO。
回答by acidbeast
Try to select PageViewController in storyboard and uncheck "Under Bottom Bars" and "Under Opaque Bars" in Attributes Inspector.
尝试在故事板中选择 PageViewController 并在属性检查器中取消选中“Under Bottom Bars”和“Under Opaque Bars”。
回答by CalebDavis
this is my first time posting on stack overflow, but I have searching for a solution to this problem for over a week now.
这是我第一次在堆栈溢出上发帖,但我已经为这个问题寻找了一个多星期的解决方案。
Here is a solution I came up with, I hope this works for anyone else with the same issue.
这是我想出的解决方案,我希望这适用于其他有同样问题的人。
I'm not sure how you are initializing your frame for your detail view controller, but I am going to assume you might use: self.view.frame.size.height;
我不确定您如何为详细视图控制器初始化框架,但我假设您可能会使用: self.view.frame.size.height;
try using: self.view.frame.size.height -= self.navigationController.navigationBar.bounds.size.height;
尝试使用: self.view.frame.size.height -= self.navigationController.navigationBar.bounds.size.height;
Hope this helps
希望这可以帮助
回答by osxdirk
As stated by "Bo:": Putting self.edgesForExtendedLayout = UIRectEdgeNone; in the viewDidLoad of MyPageViewController solved the problem. – Bo
正如“博:”所说:把 self.edgesForExtendedLayout = UIRectEdgeNone; 在 MyPageViewController 的 viewDidLoad 中解决了这个问题。– 博