Search code examples
iosswiftstatusbar

IOS 7 get the wrong status bar height


I try that the ios 7 will get the wrong height of status bar when the device rotated in swift. I have tested IPad Air (ios 7.0) and IPhone 6 (ios 8.2). It there any solution that can get the correct height of status bar?

Problem Coding(which it get self.view.bounds.size.width):

let status_height = UIApplication.sharedApplication().statusBarFrame.size.height

Solution

  • The status bar height will change when rotate,So

    Option 1

    check statusBarOrientationfirst

    UIApplication.sharedApplication().statusBarOrientation.
    

    When in portrait Use this

    let status_height = UIApplication.sharedApplication().statusBarFrame.size.height
    

    When in landscape Use this

     let status_height = UIApplication.sharedApplication().statusBarFrame.size.width
    

    Option 2 Converted it to the right coordinate In the ViewController you want to get height

         var statusBarFrame = self.view.window?.convertRect(UIApplication.sharedApplication().statusBarFrame, fromView: self.view)
         var height = statusBarFrame?.height