Xcode Project : Show SafeArea in Simulator

Here is a knock-off Xcode (Swift) project that will show the SafeArea bounds for any device in the iOS Simulator.

The simulator has a minor bug for iPhones without a physical home button, in that this app will incorrectly show Portrait Upside Down, if that is the mode the simulator is in when the app launches. If you rotate to from PortUpsidedown, this app will tell you that orientation is not supported, which is correct.

Here is all the code… .just create an Xcode Project (not Swift UI) and copy this as the only ViewController… Other than that, I’m supplying this AS-IS

//
//  ViewController.swift
//  safearea
//
//  Created by David Sisemore on 16-Jul-2021
//  Copyright © 2021 sisemore. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
	private var the$View     = UIView(frame:CGRect.zero)
	private var safeAreaView = UIView()
	private var message      = UITextView()
	required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
	//
	init() { super.init(nibName: nil, bundle: nil)
		the$View=UIView(frame:UIScreen.main.bounds)
		the$View.frame = UIScreen.main.bounds
		the$View.backgroundColor = UIColor.red
	}
	override func viewWillAppear(_ animated: Bool) {
		super.viewWillAppear(animated)
		NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
	}
	//
	override func viewWillDisappear(_ animated: Bool) {
		super.viewWillDisappear(animated)
		NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
	}

    override func viewDidLoad() {
        super.viewDidLoad()
		safeAreaView=UIView(frame:CGRect(x:0,y:0,width:100,height:100))
		safeAreaView.layer.borderColor   = UIColor.black.cgColor
		safeAreaView.layer.borderWidth   = 0.5
		safeAreaView.backgroundColor     = UIColor.systemGreen
		the$View.addSubview(safeAreaView)
		//
		message=UITextView(frame:CGRect(x:0,y:0,width:300,height:240))
		message.layer.borderColor         = UIColor.black.cgColor
		message.layer.borderWidth         = 2
		message.text                      = "text"
		message.textAlignment             = .left
		message.font                      = UIFont(name:"Menlo",size:18)
		message.isUserInteractionEnabled  = false
		safeAreaView.addSubview(message)
		self.view=the$View
	}

	override var shouldAutorotate : Bool { return true }

    override func viewDidAppear(_ animated: Bool) { showSafeArea() }

	override var prefersStatusBarHidden: Bool { return true }

	@objc func rotated() { showSafeArea() }

    private func showSafeArea() {
		var d : String=""
		var o : String=""
        if #available(iOS 11.0, *) {
			// note : all values are CGFloat, but cast as Int for display
			//
             let newSafeArea = UIApplication.shared.windows[0].safeAreaInsets
			var dw : Int = 0
			var dh : Int = 0
            let tw = Int(UIScreen.main.bounds.width)
            let th = Int(UIScreen.main.bounds.height)
			let sc = Int(UIScreen.main.scale)
            var s  : String
            let x  = Int(newSafeArea.left)
            let y  = Int(newSafeArea.top)
            var w  = Int(newSafeArea.right)
            var h  = Int(newSafeArea.bottom)

            switch UIDevice.current.orientation {
				case .landscapeLeft      : o="Landscape"; d="Left"      ; dw=max(tw,th); dh=min(tw,th);
				case .landscapeRight     : o="Landscape"; d="Right"     ; dw=max(tw,th); dh=min(tw,th);
				case .portrait           : o="Portrait";  d="Normal"    ; dw=min(tw,th); dh=max(tw,th);
				case .portraitUpsideDown : o="Portrait";  d="Upsidedown"; dw=min(tw,th); dh=max(tw,th);
				default : s="???"
			}
			w=dw-w-x; h=dh-h-y
			s="DEVICE\n"
				+ "Physical : \(dw*sc) x \(dh*sc)\n"
				+ "Logical  : \(dw) x \(dh)\n"
				+ "Scale    : \(sc)\n"
				+ "\nSAFEAREA\n"
				+ "Orient   : \(o)\n"
				+ "Direction: \(d)\n"
			//
			// Most iPhones no longer support Port-UpsideDown
			// Note : the simulator will sometimes say it does
			//        depending on how/when you rotate the device
			if tw>th &&  UIDevice.current.orientation == .portraitUpsideDown {
				s="\(s)\n*** NOT SUPPORTED ***"
				safeAreaView.frame=CGRect(x:44,y:44,width:tw-88,height:th-88)
				message.textColor = UIColor.red
			} else {
				s="\(s)"
					+ "Origin   : \(x) , \(y)\n"
					+ "Size     : \(w) x \(h)"
				safeAreaView.frame=CGRect(x:x,y:y,width:w,height:h)
				message.textColor = UIColor.black
			}
			message.text = s
			let xx=(safeAreaView.frame.width-message.frame.width)/2
			let yy=(safeAreaView.frame.height-message.frame.height)/2
			message.frame.origin=CGPoint(x:xx,y:yy)
        }
    }
}