【问题标题】:UITableViewController and UIStatusbarUITableViewController 和 UIStatusbar
【发布时间】:2016-09-26 09:39:19
【问题描述】:

在我的 UITableViewController 中滚动几个项目会使单元格出现在我的UIStatusBar 后面。我尝试使用此方法解决问题:

var bounds = self.view.bounds
    bounds.origin.y -= 20.0;
    self.view.bounds = bounds
    // Create a solid color background for the status bar



    let statusFrame = CGRect(x: 0, y: -20, width: bounds.size.width, height: 20)
    let statusBar = UIView(frame: statusFrame)
    statusBar.backgroundColor = UIColor.red
    self.view.addSubview(statusBar)

这将在滚动之前给我一个红条。它在滚动一点后消失。我还尝试将 tableView 的 contentInset 设置为UIApplication.shared.statusBarFrame.height,但也做了同样的事情。

保留导航栏很重要。我的 TableViewController 也嵌入在 UINavigationController

有什么想法吗?

【问题讨论】:

  • 滚动结束的状态,还是隐藏栏还是显示?你的平台运行的是什么,只有 iOS 8 以上还是所有版本?
  • ios8 及以上。它需要始终保持稳定。是否滚动。 @罗南
  • 你只是想把状态栏的背景颜色改成红色,并确保它不会自动改变,对吧?
  • @ronan 不,它是红色的并不重要。它只需要是实心/不透明的,这样人们就看不到它后面的细胞。
  • self.navigationController?.navigationBar.isTranslucent = false 怎么样?

标签: ios uitableview uistatusbar


【解决方案1】:

一种方法是,重构 UITableViewController 并使其成为 UITableView 并在状态栏下方添加一个顶部约束。

另一个是改变状态栏的颜色,比如:

func setStatusBarBackgroundColor(color: UIColor) {
    if let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView {
        statusBar.backgroundColor = color
    }
}

但要小心,因为它是私有 api。您的应用可能会被拒绝。

【讨论】:

  • 我不确定如何在 swift 中使用等价物。就像使用 double valueForKey
  • 我在 statusBar 上收到 NSUnknownKeyException
  • @JVS,请仔细检查拼写。
【解决方案2】:

根据您的目的,您可以隐藏状态栏。

override var prefersStatusBarHidden: Bool {
    return true
}

【讨论】:

    【解决方案3】:

    终于找到答案了:

    创建一个自定义UIView 并将其添加到navigationController 的视图中:

        let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width,
        height: UIApplication.shared.statusBarFrame.height)
    
        let bar = UIView(frame: rect)
        bar.backgroundColor = UIColor.white
    
        navigationController?.view.addSubview(bar)
    

    像魅力一样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多