How to programmatically get iOS status bar height
[UIApplication sharedApplication].statusBarFrame.size.height
. But since all sizes are in points, not in pixels, status bar height always equals 20.
Update. Seeing this answer being considered helpful, I should elaborate.
Status bar height is, indeed, equals 20.0f points except following cases:
- status bar has been hidden with
setStatusBarHidden:withAnimation:
method and its height equals 0.0f points; - as @Anton here pointed out, during an incoming call outside of Phone application or during sound recording session status bar height equals 40.0f points.
There's also a case of status bar affecting the height of your view. Normally, the view's height equals screen dimension for given orientation minus status bar height. However, if you animate status bar (show or hide it) after the view was shown, status bar will change its frame, but the view will not, you'll have to manually resize the view after status bar animation (or during animation since status bar height sets to final value at the start of animation).
Update 2. There's also a case of user interface orientation. Status bar does not respect the orientation value, thus status bar height value for portrait mode is [UIApplication sharedApplication].statusBarFrame.size.height
(yes, default orientation is always portrait, no matter what your app info.plist says), for landscape - [UIApplication sharedApplication].statusBarFrame.size.width
. To determine UI's current orientation when outside of UIViewController
and self.interfaceOrientation
is not available, use [UIApplication sharedApplication].statusBarOrientation
.
Update for iOS7. Even though status bar visual style changed, it's still there, its frame still behaves the same. The only interesting find about status bar I got – I share: your UINavigationBar
's tiled background will also be tiled to status bar, so you can achieve some interesting design effects or just color your status bar. This, too, won't affect status bar height in any way.
How to get the status bar height in iOS 13?
As the warning hints, you can access the statusBarManager
which has a statusBarFrame
property. This is defined on your UIWindow
's windowScene
.
let height = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
Status bar height in Swift
Is there any problems with Swift 2.x:
UIApplication.sharedApplication().statusBarFrame.size.height
Swift 3 or Swift 4:
UIApplication.shared.statusBarFrame.height
Make sure UIKit
is imported
import UIKit
In iOS 13, you will get a deprecated warning"
'statusBarFrame' was deprecated in iOS 13.0: Use the statusBarManager
property of the window scene instead.
To fix this:
let height = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
How to set the status bar height to 20pt when on a call like the App Store does?
this is really an interesting question, took me sometimes to figure it out.
if you notice app store in such viewcontroller didn't have a status bar, which means the green bar should not be shown at all. this is the first hint.
you can access the status bar by
UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow
according to point number one this means the status bar is not hidden but actually its
statusBarWindow.frame.origin.y
just shifted up.please not you need to handle iPhone x separately
please make sure the status bar is not hidden
note that this is not the only right way, right now you have the view itself, you can change the origin or size or even try to get what inside this view and hide them or change their frames too etc.
here is an example how you can do it.
class ViewController: UIViewController {
var isHidden:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 0.5) { () -> Void in
let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow
statusBarWindow.frame = CGRect(x: 0, y: -20, width: statusBarWindow.frame.size.width, height: 40.0)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override var prefersStatusBarHidden: Bool{
return isHidden
}
}
also attached an image with the result of this code, hopefully, this answer work for you too.
Programmatically get height of navigation bar
The light bulb started to come on. Unfortunately, I have not discovered a uniform way to correct the problem, as described below.
I believe that my whole problem centers on my autoresizingMasks. And the reason I have concluded that is the same symptoms exist, with or without a UIWebView. And that symptom is that everything is peachy for Portrait. For Landscape, the bottom-most UIButton pops down behind the TabBar.
For example, on one UIView, I have, from top to bottom:
UIView – both springs set (default case) and no struts
UIScrollView -
If I set the two springs, and clear everything else (like the UIView), then the UIButton intrudes on the object immediately above it.
If I clear everything, then UIButton is OK, but the stuff at the very top hides behind the StatusBar
Setting only the top strut, the UIButton pops down behind the Tab Bar.
UILabel and UIImage next vertically – top strut set, flexible everywhere else
Just to complete the picture for the few that have a UIWebView:
UIWebView -
Struts: top, left, right
Springs: both
UIButton – nothing set, i.e., flexible everywhere
Although my light bulb is dim, there appears to be hope.
Related Topics
Change Width of a Uibarbuttonitem in a Uinavigationbar
Download and Cache Images in Uitableviewcell
Wrap Items in a Horizontal Uistackview on Multiple Lines
How to Add a Button to Uinavigationbar
Xcode 6 Save for Enterprise Deployment Does Not Create Plist for IPA Anymore
Ios: Add Imageview in a Scrollview to Have Zoom
How to Create Static Library and Can Add Just .A File on Any Project in iOS
iOS Navigation Bar Item Image Size
How to Really Remove Copy from Uimenucontroller
Animating/Moving Views Under Usage of Autolayout
Singleton in iOS Objective C Doesn't Prevent More Than One Instance
iOS - Swift - Function That Returns Asynchronously Retrieved Value
Eraser Not Working in iOS Drawing
Dial a Phone Number with an Access Code Programmatically in iOS
Facebook Graph API Get Request - Should Contain "Fields" Parameter (Swift, Facebook Sdk V4.5.1)
How to Mimic Keyboard Animation on iOS 7 to Add "Done" Button to Numeric Keyboard