-1

I want to be able to add a small view behind the status bar or set the backgroundColor of the status bar. I have seen it in a couple of apps.

How do you add a backgroundColor to the status bar in swift?

FeReTu
  • 125
  • 2
  • 11

1 Answers1

0

How do you add a backgroundColor to the status bar

You don't. The status bar is transparent. You put something behind it that is the color you want! If your "something" is a 20-pixel-tall view with its top pinned to the top of the screen (i.e. the top of the root view controller's main view), it will exactly match the position of the status bar. There is no magic sauce for this; you just put a view, like any other view, in the right spot in your interface, where you know the status bar will end up in front of it.

Be sure to remove the backing view in situations where the status bar is removed (e.g. landscape). You can track status bar visibility with a notification.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • However, I should add that you really should _not_ be doing this. The look you get when you do that is "old school" - this is how apps looked in iOS 7, when the status bar _did_ have an actual background color. Nowadays, apps are supposed to look "fullscreen". The apps that do what I've just described are actually behaving badly. – matt Oct 01 '16 at 15:14
  • So you're saying that the status bar should be removed? – FeReTu Oct 01 '16 at 15:29
  • No. Where do you see me "saying" that? I'm saying that the status bar just appears over the top 20 pixels of your full-screen interface, with no delineation color such as you are asking for. You _can_ do the 20-pixel background, but you are expected not to. – matt Oct 01 '16 at 15:37
  • It's just that the status bar is irritating more than it should when there is no background, in my app, because there are multiple custom tableview cells.. It would be much better with the background :/ – FeReTu Oct 04 '16 at 15:16
  • In a situation like that, you are expected to be in a navigation interface. The top of your interface will be the navigation bar, and the status bar will appear in front of that. We should _never_ see table view cells passing behind the status bar. – matt Oct 04 '16 at 15:18