在我的iOS应用程序(使用Xamarin开发)中,我使用自定义容器视图控制器在与UISegmentedControl段相关联的不同子视图控制器(它们都包含一个UITableView)之间切换。
一开始,当切换到新的视图控制器时,我遇到了错误的contentInset分配问题,因为视图位于导航栏下面。我用另一个堆栈溢出question中提出的解决方案解决了这个问题,它的工作原理就像魅力一样。
现在的问题是,一个子视图控制器以模式方式呈现另一个视图控制器,当它被取消时,contentInset再次被设置错误。在本例中,表视图从可用空间的中间开始,在导航栏和第一行之间有相当大的空白。
我试图理解是哪种方法隐式地更改了内容嵌入,但我没有运气。那么,这种行为的罪魁祸首是什么呢?
我尝试过将automaticallyAdjustsScrollViewInsets设置为true和false,但结果是一样的。
更新:似乎这个问题只出现在iOS 8中,而不是在iOS 7中。
发布于 2015-08-06 07:59:11
Synposis
摆脱contentInset。在分段控制视图控制器和每个表视图控制器之间插入一个UINavigationController。
在故事板中的样子
虽然您不需要使用Storyboard来实现此解决方案,但它提供了非常图形化的用户界面。

连接分段控制
同样,为了简单明了,我使用了Storyboard。欢迎您以编程方式创建每个导航控制器和视图控制器(如果适合的话)。
这是整个类。总共30行代码。
class SegmentedControlViewController: UIViewController {
var segmentViewController:UIViewController? = nil
override func viewDidLoad() {
super.viewDidLoad()
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("nav0") as? UIViewController
self.addChildViewController(vc!)
self.view.addSubview(vc!.view)
self.segmentViewController = vc
}
@IBAction func segmentedControlValueChanged(sender: AnyObject) {
if let segmentedControl = sender as? UISegmentedControl {
let identifier = "nav\(segmentedControl.selectedSegmentIndex)"
let vc = self.storyboard!.instantiateViewControllerWithIdentifier(identifier) as? UIViewController
self.addChildViewController(vc!)
self.transitionFromViewController(self.segmentViewController!, toViewController: vc!, duration: 0, options: .TransitionNone, animations: { () -> Void in
self.segmentViewController!.view.removeFromSuperview()
//vc!.view.frame = self.view.bounds
self.view.addSubview(vc!.view)
}, completion: { (Bool) -> Void in
vc!.didMoveToParentViewController(self)
self.segmentViewController!.removeFromParentViewController()
self.segmentViewController = vc
})
}
}
}Compatibility
上面的代码没有显示如何使用Segue Present Modally或`Show (例如Push)来推送视图控制器,因为它不在此响应的范围之内,并且两者都已被验证。
在iPhone 4s到6 Plus,每个iPad,肖像,景观,方向变化,iOS 7和8上建立和测试。
https://stackoverflow.com/questions/31671387
复制相似问题