0

Scrolling in my UITableViewController with several items makes the the cell appear behind my UIStatusBar. I tried to do a work around using this method:

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)

which will give me a red bar before scrolling. It disappears after a little bit of scrolling. I also tried to make the contentInset of the tableView top to UIApplication.shared.statusBarFrame.height, but did the same.

It is important to keep the Navbar. Also my TableViewController is embedded in a UINavigationController

Any ideas?

JVS
  • 2,592
  • 3
  • 19
  • 31
  • What about the state when finishing scrolling, Still hide the bar or show? And what is your platform running, only iOS 8 above or all version? – ronan Sep 26 '16 at 09:47
  • ios8 and above. It needs to be solid all the time. whether scrolling or not. @ronan – JVS Sep 26 '16 at 09:49
  • You just wanna change to status bar's background color to red, and make sure it won't be changed automatically, right? – ronan Sep 26 '16 at 09:53
  • @ronan no it's not important, that it is red. It just needs to be solid/opaque so that one can't see the cells behind it. – JVS Sep 26 '16 at 10:02
  • 1
    how about `self.navigationController?.navigationBar.isTranslucent = false`? – ronan Sep 26 '16 at 10:08
  • @ronan it does make a black stripe instead of the statusbar. but the behavior seems to be good. any idea how to make this stripe white ? – JVS Sep 26 '16 at 10:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124196/discussion-between-ronan-and-jvs). – ronan Sep 26 '16 at 10:16

3 Answers3

1

One way could be, refactor UITableViewController and make it UITableView and add a top constraint below the status bar.

Another is to change the status bar color like:

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

But be careful since it is a private api. Your app might get rejected.

Xchord
  • 678
  • 1
  • 5
  • 20
1

According to your purpose, You could just hide the status bar.

override var prefersStatusBarHidden: Bool {
    return true
}
ronan
  • 1,611
  • 13
  • 20
0

Finally found an answer:

create a custom UIView and add it the the view of 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)

works like a charm.

JVS
  • 2,592
  • 3
  • 19
  • 31