[ios] How to programmatically get iOS status bar height

While the status bar is usually 20pt tall, it can be twice that amount in some situations:

  • when you're in the middle of a phone call (that's a pretty common scenario);
  • when the voice recorder, or Skype, or a similar app, is using the microphone in the background;
  • when Personal Hotspot is activated;

Just try it, and you'll see for yourself. Hardcoding the height to 20pt will usually work, until it doesn't.

So I second H2CO3's answer:

statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;