NavigationBar를 추가 하려고 했는데 IOS 버전별 statusBarHeight가 방식에 따라 다르게 나온다.

 

노치 없는 7 Plus 모델(iOS 11.0)과 같은 기기에서 NaviBar 배치시 y값을 0으로 하면 시계표시 부분과 겹친다. 

시계 영역의 StatusBar 영역의 값을 구해야 되는데 기존에 사용하던

UIApplication.shared.windows.first?.safeAreaInsets.top 코드로 하면 0이 나온다.

iOS 버전 11.0 이었는데 해당 13버전 미만은

UIApplication.shared.statusBarFrame.size.height 코드로 해야 정상적으로 20으로 정상 표시된다.

private var naviBar: UINavigationBar?

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    
    // navigationBar 추가
    var statusBarHeight: CGFloat = 0
    if #available(iOS 13.0, *) {
        statusBarHeight =
        UIApplication.shared.windows.first?.safeAreaInsets.top ?? 0
    } else {
        // Fallback on earlier versions
        // iOS 11에서는 해당 코드로 statusBar Height 구해야 정상적으로 나온다. ㅜㅜ
        statusBarHeight = UIApplication.shared.statusBarFrame.size.height
    }
    
    naviBar = UINavigationBar(frame: .init(x: 0, y: statusBarHeight, width: view.frame.width, height: 40))
    
    naviBar?.isTranslucent = false
    naviBar?.backgroundColor = .white
    
    let naviItem = UINavigationItem(title: "미리보기")
    naviBar?.items = [naviItem]
    naviItem.rightBarButtonItem = UIBarButtonItem(title: "닫기", style: .plain, target: self, action: #selector(closeButton))
    
    view.addSubview(naviBar!)
}

@objc func closeButton(_ sender: UIButton) {
}

+ Recent posts