UINavigation嵌套UIpageViewController时视图会下移到UInavigationBar下面
2015-04-08 00:02
417 查看
Content
pushed down in a UIPageViewController with UINavigationController
up vote24down votefavorite 8 | UPDATE 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? UPDATE 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. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ My window's rootViewController is being set to a UINavigationController whose rootViewController has a UIPageViewController embedded. 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. In my appDelegate: self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ContainerViewController new]]; In 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]; } iphone ios objective-c uinavigationcontroller uipageviewcontroller
| ||||||||
a comment |
7 Answers
activeoldestvotesup vote0down vote | 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; try using: self.view.frame.size.height -= self.navigationController.navigationBar.bounds.size.height; Hope this helps
| ||
add a comment |
up vote1down vote | 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.
| ||
add a comment |
up vote0down vote | As stated by "Bo:": Putting self.edgesForExtendedLayout = UIRectEdgeNone; in the viewDidLoad of MyPageViewController solved the problem. – Bo
| ||
add a comment |
up vote5down vote | This is definitely being caused by automaticallyAdjustsScrollViewInsets, as other posters (including @djibouti33). However, this property is strange in two ways: It must be set on a UINavigationController. If you set it on a child controller that's managed by a UINavigationController, it won't have any effect. 1 It only applies when a scroll view is at index zero in a controller's subviews. 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 UIPageViewControllerat index zero, to avoid the setting applying to the scrollView within the page controller, like this: pageViewController.view.insertSubview(UIView(), atIndex: 0) // swift [pageViewController.view insertSubview: [UIView new] atIndex: 0]; // obj-c Better would be to set the contentInseton the scroll view yourself, but unfortunately the UIPageViewControllerdoesn't expose the scroll view.
| ||||||||||||
a comment |
up vote22down voteaccepted | 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 automaticallyAdjustsScrollViewInsetsto false. Here's my viewDidLoadnow: - (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).
| ||||
a comment |
up vote3down vote | 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: -- UINavigationController -- MyPageViewController -- MyDetailViewController Here's what I did to solve it: In 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]; }
| ||||||||||||
a comment |
up vote0down vote | Initially my view controller hierarchy looked like this:-- UINavigationController -- MyContainerViewController -- UIPageViewController -- MyDetailViewController I set it up this way so MyContainerViewController could manage a toolbar. I narrowed my problem down to MyContainerViewController, and then it occurred to me that I don't even need it if I subclass UIPageViewController. Now my hierarchy looks like this: -- UINavigationController -- MyPageViewController -- MyDetailViewController MyPageViewControllermanages it's toolbar, and everything works as expected, both on a 4-inch and 3.5-inch device.
| ||||||||
|
相关文章推荐
- 试图添加进 ScrollerVier 的视图里,默认下移64个像素
- iOS开发之高级视图——UINavigationBar
- ios7 uitableview\uiscrollview内容视图下移
- 去掉UINavigationBar下面黑线方法
- 让UIView、UIWindow等透明遮挡不影响下面视图操作方法
- swift中UI适配,即视图控件在导航栏下面开始显示
- DDMS视图下面打不开data的解决方式
- 让UIView、UIWindow等透明遮挡不影响下面视图操作方法
- 将原点移动到navigationBar下面(或UIScrollView和 cell加载子视图偏移64问题)
- ios7 uitableview内容视图下移 20px bug
- iphone中UINavigationBar(导航条视图)的介绍
- ios-将代码创建的视图控件放入拖拽控件的下面
- 修改IOS中UISearchBar的取消按钮及所有视图看了其他的文章都不试一下就发出来,更本就存在些纰漏,下面是我做了稍许修改的结果:
- 为什么GridLayout下面会多出一片空白区域?并且还遮盖了我的一个视图
- iOS 将原点移动到navigationBar下面(或UIScrollView和 cell加载子视图偏移64问题)
- 实战:把ASP.NET MVC中的Views下面的视图放到Views文件夹外
- 让覆盖在下面层的视图接受触摸事件
- 让UIView、UIWindow等透明遮挡不影响下面视图操作方法
- swift中UI适配,即视图控件在导航栏下面开始显示
- 隐藏头视图即隐藏UINavigationBar