繁体   English   中英

UITableViewController和UIStatusbar

[英]UITableViewController and 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 ,但操作相同。

保持导航栏很重要。 另外我的TableViewController嵌入在UINavigationController

有任何想法吗?

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

另一种是更改状态栏颜色,例如:

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

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

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

override var prefersStatusBarHidden: Bool {
    return true
}

终于找到了答案:

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

奇迹般有效。

暂无
暂无

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

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