【问题标题】:TableView content inset in UITabBarControllerUITabBarController 中的 TableView 内容插图
【发布时间】:2014-03-24 09:11:18
【问题描述】:

我在 UITabBarController 中有 4 个 UITableViewController。它们都使用相同的 UITableViewController 类,但在最后 3 个控制器上,tableView 没有正确显示:tableView 的顶部显示在 navigationBar 下,而第一个显示正确

我尝试通过以编程方式设置“MCVigilanceSingleColorViewController”类(继承自 UITableViewController)中的 tableView contentInset 来解决问题,但它应用于我的四个控制器(然后我的第一个 UITableViewController 的 tableView 现在有一个错误的框架...... )。

在故事板中,我的 4 个 UITableViewController 也使用相同的类,而且它们都相同:我的 4 个 ViewController 完全相同。第 2、第 3 和第 4 个控制器是第一个控制器的“复制/粘贴”(我刚刚更改了 barItemText)。然后他们都选中和未选中相同的选项。顺便说一句,选中“underTopBard”选项对我显示的控制器没有影响。我只是不明白为什么它们的显示方式不同。

你能帮帮我吗?谢谢。

【问题讨论】:

  • 检查您是否在第一个视图控制器上选中了“在顶部和底部栏下扩展”
  • 我忘了说,在storyboard里,我的4个ViewController是一模一样的。第 2、第 3 和第 4 个控制器是第一个控制器的“复制/粘贴”(我刚刚更改了 barItemText)。然后他们都选中和未选中相同的选项。顺便说一句,选中此选项对我显示的控制器没有影响。

标签: ios iphone objective-c uitableview uitabbarcontroller


【解决方案1】:

我遇到了同样的问题,如果UIViewController 包含在UITabBarController 中,这听起来像是与automaticallyAdjustsScrollViewInsets 相关的错误。我通过将以下代码添加到我的viewDidLoad 来解决它:

self.automaticallyAdjustsScrollViewInsets = NO;

CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
self.internalTableView.contentInset =
  self.internalTableView.scrollIndicatorInsets =
    UIEdgeInsetsMake(statusBarHeight + navigationBarHeight, 0, tabBarHeight, 0);

其中internalTableViewUIScrollView,例如UITableView

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,在没有找到真正的正确原因后,我决定进行“破解”来为我修复它...

    这就是我添加这个的原因,它很快,但您可以轻松地将其转换为objective-c:

        var navBarHeight:CGFloat = 0
        if let nc = self.navigationController {
            navBarHeight += nc.navigationBar.frame.height
            navBarHeight += UIApplication.sharedApplication().statusBarFrame.size.height
        }
    
        //iterate over vc's
        if let vc = self.viewControllers {
            //enumerrate allows for the tuple, setting a key
            for (key, c) in enumerate(vc) {
                //first view displays correctly
                if (key > 0) {
                    if let controller = c as? BaseListViewController {
                        controller.automaticallyAdjustsScrollViewInsets = false
                        controller.tableView.contentInset = UIEdgeInsets(
                            top: navBarHeight,
                            left: 0,
                            bottom: 0,
                            right: 0
                        )
                    }
                }
            }
        }
        super.viewDidLoad()
    

    这是在我的 UITabBar 的基类中(我显然扩展了它)。 基本上它检查是否有多个选项卡应该有第二个,++ 将调整它们的 contentInsets 以在顶部有额外的,类似于导航栏 + 状态栏。

    我希望这对某人有所帮助。

    【讨论】:

      【解决方案3】:

      将 UITableViewController 嵌入 UINavigationController 为我解决了这个问题。从情节提要中这很容易。只需选择 UITableViewController,然后转到 Editor > Embed In > Navigation Controller。

      【讨论】:

        猜你喜欢
        • 2012-02-29
        • 2019-07-18
        • 2016-10-14
        • 1970-01-01
        • 1970-01-01
        • 2019-03-05
        • 1970-01-01
        • 1970-01-01
        • 2013-09-04
        相关资源
        最近更新 更多