选项卡更改后导航栏下的表视图

时间:2016-06-03 15:13:19

标签: ios uitableview navigationbar

我有一个带有两个标签的应用,每个标签都包含一个表视图。整个包装在UINavigationController中。这是我的故事板: enter image description here

当我运行我的应用时,第一个标签是OK:

enter image description here

但是在第二个,表格视图从导航栏开始:

enter image description here

更糟糕的是,当我从第二个标签旋转屏幕时,第二个标签现在可以了,但第一个标签已不再存在,表格视图有一个额外的边距顶部:

enter image description here

如果我从第一个选项卡旋转回纵向,我将返回初始状态(第一个选项卡确定,第二个选项卡,导航栏下方有表格视图)。实际上,每次旋转屏幕时,旋转后显示的选项卡都可以,但另一个不是。

在我更改标签后显示我的视图时,我似乎需要做一些事情,并且当屏幕旋转时似乎已经完成了这件事,但它是什么?

编辑:我忘了提到我不想取消选中"延伸边缘:在顶部/底部条形图下"复选框,因为我希望我的表视图在导航栏和标签栏下滚动。

1 个答案:

答案 0 :(得分:1)

好的,我明白了:首先取消选中"调整滚动视图插图"在UITabBarController上,然后在每个UITableViewController上添加此代码:

override func viewWillAppear(animated: Bool) {

    // Call super:
    super.viewWillAppear(animated);

    // Update the table view insets:
    updateTableViewInsets();
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    // Animate:
    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in

        // Update the table view insets:
        self.updateTableViewInsets();

    }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in })

    // Call super:
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}

private func updateTableViewInsets() {

    // Get the bars height:
    let navigationBarHeight = self.navigationController!.navigationBar.frame.size.height;
    let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height;
    let tabBarHeight = self.tabBarController!.tabBar.frame.size.height;

    // Create the insets:
    let insets: UIEdgeInsets = UIEdgeInsetsMake(navigationBarHeight + statusBarHeight, 0, tabBarHeight, 0);

    // Update the insets:
    self.tableView.scrollIndicatorInsets = insets;
    self.tableView.contentInset = insets;
}

现在我自己处理插图,一切顺利。这应该是开箱即用的恕我直言,但是......

编辑:它与iOS 9开箱即用,Apple可能已经修复了这个bug。上面的代码适用于iOS 8& 9(也许更低,我没有尝试过)。