【发布时间】:2013-10-03 01:17:28
【问题描述】:
更新
根据 Tim 的回答,我在每个视图控制器中实现了以下内容,该控制器具有作为我的自定义容器一部分的滚动视图(或子类):
- (void)didMoveToParentViewController:(UIViewController *)parent
{
if (parent) {
CGFloat top = parent.topLayoutGuide.length;
CGFloat bottom = parent.bottomLayoutGuide.length;
// this is the most important part here, because the first view controller added
// never had the layout issue, it was always the second. if we applied these
// edge insets to the first view controller, then it would lay out incorrectly.
// first detect if it's laid out correctly with the following condition, and if
// not, manually make the adjustments since it seems like UIKit is failing to do so
if (self.collectionView.contentInset.top != top) {
UIEdgeInsets newInsets = UIEdgeInsetsMake(top, 0, bottom, 0);
self.collectionView.contentInset = newInsets;
self.collectionView.scrollIndicatorInsets = newInsets;
}
}
[super didMoveToParentViewController:parent];
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我有一个名为 SegmentedPageViewController 的自定义容器视图控制器。我将其设置为UINavigationController's rootViewController。
SegmentedPageViewController 的目的是允许一个UISegmentedControl,设置为 NavController 的 titleView,在不同的子视图控制器之间切换。
这些子视图控制器都包含滚动视图、表格视图或集合视图。
我们发现第一个视图控制器加载良好,正确定位在导航栏下方。但是当我们切换到一个新的 视图控制器,导航栏不受尊重,视图设置在导航栏下方。
我们正在使用自动布局和界面构建器。我们已经尝试了所有能想到的方法,但找不到一致的解决方案。
这是负责设置第一个视图控制器并在用户点击分段控件时切换到另一个视图控制器的主要代码块:
- (void)switchFromViewController:(UIViewController *)oldVC toViewController:(UIViewController *)newVC
{
if (newVC == oldVC) return;
// Check the newVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (newVC) {
// Set the new view controller frame (in this case to be the size of the available screen bounds)
// Calulate any other frame animations here (e.g. for the oldVC)
newVC.view.frame = self.view.bounds;
// Check the oldVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (oldVC) {
// **** THIS RUNS WHEN A NEW VC IS SET ****
// DIFFERENT FROM FIRST VC IN THAT WE TRANSITION INSTEAD OF JUST SETTING
// Start both the view controller transitions
[oldVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
// Swap the view controllers
// No frame animations in this code but these would go in the animations block
[self transitionFromViewController:oldVC
toViewController:newVC
duration:0.25
options:UIViewAnimationOptionLayoutSubviews
animations:^{}
completion:^(BOOL finished) {
// Finish both the view controller transitions
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}];
} else {
// **** THIS RUNS WHEN THE FIRST VC IS SET ****
// JUST STANDARD VIEW CONTROLLER CONTAINMENT
// Otherwise we are adding a view controller for the first time
// Start the view controller transition
[self addChildViewController:newVC];
// Add the new view controller view to the view hierarchy
[self.view addSubview:newVC.view];
// End the view controller transition
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}
}
}
【问题讨论】:
-
高度限制是否设置正确?
-
我假设是这样。奇怪的是,总是第一个加载正常,第二个加载不正确。如果我切换它以便在应用程序加载时选择第二段,则加载正常,并且第一段加载不正确,与我在这里用作示例的图像相反。
-
虽然我没有明确的表视图(在 vc 1 中)或集合视图(在 vc2 中)的高度约束。在笔尖中,它们只是设置为占据视野的全部高度。事实上,这两个 xib 没有明确应用任何约束。
-
我遇到了同样的问题,这让我发疯了。感谢您花时间找出并发布您的解决方案。我无法自己解决这个问题。谢谢!
-
很好的答案!只要确保在覆盖 didMoveToParentViewController 时调用 super。我还向 Apple 提交了雷达:openradar.appspot.com/16042338
标签: ios objective-c uinavigationbar ios7