简体   繁体   English

TableView 显示在标签栏后面

[英]TableView Showing Behind Tab Bar

I am updating my app to use iOS 7 and I'm having a problem with a table view.我正在更新我的应用程序以使用 iOS 7,但我在使用表格视图时遇到了问题。 My tab bar is translucent.我的标签栏是半透明的。 The problem is when I scroll to the bottom of my table view, part of the last cell is still behind the tab bar.问题是当我滚动到表格视图的底部时,最后一个单元格的一部分仍然在标签栏后面。 I'd like to have a bit of space between the last cell and the tab bar.我想在最后一个单元格和标签栏之间留一点空间。 I could fix this by using an opaque tab bar instead, but I want to keep it translucent.我可以通过使用不透明的标签栏来解决这个问题,但我想让它保持半透明。

在此处输入图片说明

Try setting尝试设置

self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = NO;
self.automaticallyAdjustsScrollViewInsets = NO;

Inside the tableview controller在 tableview 控制器内部

Swift 4.x斯威夫特 4.x

let adjustForTabbarInsets: UIEdgeInsets = UIEdgeInsetsMake(0, 0, self.tabBarController!.tabBar.frame.height, 0)
self.yourTableView.contentInset = adjustForTabbarInsets
self.yourTableView.scrollIndicatorInsets = adjustForTabbarInsets

Check the screen shot检查屏幕截图

在此处输入图片说明

Check the under top Bar and Un-checke under Bottom Bar检查下顶部栏和取消底部栏下的检查

SWIFT 3斯威夫特 3

put this inside viewDidLoad of your tableViewController :把它放在你的tableViewController viewDidLoad中:

self.edgesForExtendedLayout = UIRectEdge()
self.extendedLayoutIncludesOpaqueBars = false
self.automaticallyAdjustsScrollViewInsets = false

Swift 3.0斯威夫特 3.0

This is what worked for me.这对我有用。 In your Custom ViewController:在您的自定义 ViewController 中:

override func viewDidLoad() {
    super.viewDidLoad()

    let adjustForTabbarInsets: UIEdgeInsets = UIEdgeInsetsMake(self.tabBarController!.tabBar.frame.height, 0, 0, 0);
    //Where tableview is the IBOutlet for your storyboard tableview.
    self.tableView.contentInset = adjustForTabbarInsets;
    self.tableView.scrollIndicatorInsets = adjustForTabbarInsets;
}

Not to sure I like the solution but it works for me.不确定我喜欢这个解决方案,但它对我有用。

With iOS 11 I have no issue, I simply use the following in viewDidLoad() :使用 iOS 11 我没有问题,我只是在viewDidLoad()使用以下内容:

self.collectionView.bottomAnchor.constraint(self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true

However on iOS 10 I need to hack my way like this:但是在 iOS 10 上,我需要像这样破解我的方法:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let tabBarHeight: CGFloat = (self.parent?.tabBarController?.tabBar.frame.size.height)!

    if #available(iOS 11.0, *) {

    } else {
        self.collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -tabBarHeight).isActive = true
    }
 }

这对我有用

override func viewDidLoad() { self.edgesForExtendedLayout = UIRectEdge() self.extendedLayoutIncludesOpaqueBars = false }

If any view shows behind a UITabBar you can grab the bottomLayoutGuide and make adjustments at runtime.如果任何视图显示在 UITabBar 后面,您可以获取 bottomLayoutGuide 并在运行时进行调整。 What I do is have a BaseViewController that all my view controllers inherit from.我所做的是有一个 BaseViewController,我的所有视图控制器都继承自它。 Then if the tab bar is visible we adjust the view like so:然后,如果标签栏可见,我们可以像这样调整视图:

import UIKit

class BaseVC: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewDidLayoutSubviews() {
    //Ensures that views are not underneath the tab bar
    if tabBarController?.tabBar.hidden == false {
        var viewBounds = self.view.bounds;
        var bottomBarOffset = self.bottomLayoutGuide.length;
        self.view.frame = CGRectMake(0, 0, viewBounds.width, viewBounds.height - bottomBarOffset)
    }
  }
}

Since I don't use storyboards (where you can click a checkbox in IB to fix this problem), this has been the best solution I have found.由于我不使用故事板(您可以单击 IB 中的复选框来解决此问题),因此这是我找到的最佳解决方案。

It is really hard to resolve the issue without detail information or actual codes.如果没有详细信息或实际代码,很难解决问题。 I have similar issue of tabview behind UItabBar in my project.我的项目中 UItabBar 后面有类似的 tabview 问题。 The solutions offered here do not work in my case.这里提供的解决方案在我的情况下不起作用。 After exploring my codes, I found a solution for my case.在探索了我的代码之后,我找到了适合我的案例的解决方案。

Here is brief explanation of my case.这是我的案例的简要说明。 I have a UItabBar in main view with two tab buttons.我在主视图中有一个带有两个选项卡按钮的 UItabBar。 In one tab view, there is table view.在一个选项卡视图中,有表格视图。 If user taps on a row, a detail view is presented by using navigation controller.如果用户点击一行,则使用导航控制器显示详细信息视图。 In the detail view, the tab bar is hidden, and a toolbar is showing at the bottom.在详细视图中,标签栏被隐藏,工具栏显示在底部。

In order to bring tab bar back and hide the toolbar when the main view is brought back, I have to explicitly show tab bar and hide toolbar in the event of viewWillAppear:为了在主视图被带回时带回标签栏并隐藏工具栏,我必须在 viewWillAppear 事件中显式显示标签栏和隐藏工具栏:

class myMainViewController: UITableViewController {
  private var tabBarHidden: Bool? = {
    didSet {
      self.tabBarController?.tabBar.isHidden = tabBarIsHidden ?? true
    }
  }

  private var toolBarIsHidden: Bool? {
    didSet {
      let hidden = toolBarIsHidden ?? true
        self.navigationController?.toolbar.isHidden = hidden
        self.navigationController?.setToolbarHidden(hidden, animated: true)
      }
  }
  ...
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.tabBarIsHidden = false
    self.toolBarIsHidden = true
  }
  ...
}

I finally realize that the visibility of bar at the bottom is set in the event of viewWillAppear.我终于意识到底部栏的可见性是在 viewWillAppear 事件中设置的。 At that time, the tableView or scroll view's content insets are set already based on no bar at the bottom.那时,tableView 或滚动视图的内容插入已经基于底部没有栏设置。 That's why my tableView is behind the bottom bar.这就是为什么我的 tableView 在底部栏后面。

The solution I found is to reset content insets in the event of viewDidAppear:我找到的解决方案是在 viewDidAppear 事件中重置内容插入:

override func viewDidAppear(_ animated: Bool) {
  // In the event of viewWillAppear, visibilities of tool bar and tab bar are set or changed,
  // The following codes resets scroll view's content insets for tableview
  let topInset = self.navigationController!.navigationBar.frame.origin.y +
    self.navigationController!.navigationBar.frame.height
  let adjustForTabbarInsets: UIEdgeInsets = UIEdgeInsetsMake(
        topInset, 0,
        self.tabBarController!.tabBar.frame.height, 0)
  self.tableView.contentInset = adjustForTabbarInsets
  self.tableView.scrollIndicatorInsets = adjustForTabbarInsets
}

The best approch would be to Embed TabBarController to your ViewController (Editor -> Embed In -> TabBar Controller)and set the bottom of the tableview to be bottom of safe area of viewcontroller.最好的方法是将 TabBarController 嵌入到您的 ViewController(编辑器 -> 嵌入 -> TabBar 控制器)并将 tableview 的底部设置为 viewcontroller安全区域的底部。 The other ways wont be as perfect as this one.其他方式不会像这种方式那么完美。

You need to adjust the height of the table view.您需要调整表格视图的高度。 Just leave 49px at the bottom, as the tabbar height is 49 px.只需在底部留下 49 像素,因为标签栏高度为 49 像素。 Adjust the height of table view so that it leaves 49px space below it.调整 table view 的高度,使其下方留出 49px 的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM