UITableViewController和UIStatusbar

时间:2016-09-26 09:39:20

标签: ios uitableview uistatusbar

使用多个项目滚动我的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,但也做了同样的事情。

保留Navbar非常重要。我的TableViewController也嵌入在UINavigationController

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

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

另一种方法是更改​​状态栏颜色,如:

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

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

答案 1 :(得分:1)

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

override var prefersStatusBarHidden: Bool {
    return true
}

答案 2 :(得分:0)

终于找到了答案:

创建自定义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)

就像一个魅力。