How to get safe area height in swift

To get the safe area height in Swift, you can use the safeAreaLayoutGuide property of the UIView class. This property provides access to the layout guide representing the area in which your content should be safely displayed.

Example:

      import UIKit

      class ViewController: UIViewController {

         override func viewDidLoad() {
            super.viewDidLoad()
            
            let safeAreaHeight = view.safeAreaLayoutGuide.layoutFrame.size.height
            print("Safe Area Height: \(safeAreaHeight)")
         }
      }
      
   

In the example above, we are accessing the safe area layout guide through the safeAreaLayoutGuide property of the view property of UIViewController. We then retrieve the height of the layout frame of the safe area using the layoutFrame.size.height property.

By printing the value of safeAreaHeight, you will get the height of the safe area in points. This is useful for ensuring that your content is properly displayed within the safe area of the device’s screen.

Leave a comment