ios – Xcode 26 (beta 6), iPadOS 26: StatusBar overlaps with NavigationBar after offered viewController rotates and dismissed

0
1
ios – Xcode 26 (beta 6), iPadOS 26: StatusBar overlaps with NavigationBar after offered viewController rotates and dismissed


Please see the total reply under. You probably have additional questions, please message me or remark under. When you want additional sources, seek the advice of this hyperlink: www.youtube.com/@LavaLaboratories Thanks! Lachlan

It is a basic layout-timing problem in iOS, notably on iPadOS, the place the UINavigationBar‘s structure is not up to date appropriately in sync with the standing bar’s cover/present animation.

The issue happens as a result of the guardian view controller is not being prompted to re-evaluate the standing bar’s look when its offered view controller is dismissed. Equally, the offered view controller’s navigation bar calculates its preliminary structure earlier than the standing bar is totally hidden, leading to that further area.

To repair this, that you must manually set off a standing bar look replace on the proper moments within the view controller lifecycle.


## The Answer: Triggering Handbook Updates

You will must make small additions to each of your view controllers.

  1. In ParentViewController: When the offered view is dismissed, the guardian is about to look. It’s worthwhile to inform the system to re-evaluate the standing bar look at this second.

  2. In PresentedViewController: When this view is about to look, explicitly inform it to replace its standing bar desire. This helps its navigation controller calculate the right preliminary structure.


## Corrected Code

Listed below are the up to date recordsdata with the mandatory modifications.

ParentViewController.swift

Add the viewWillAppear methodology to your ParentViewController. This can repair the overlapping standing bar if you dismiss the offered view.

Swift

import UIKit

class ParentViewController: UIViewController {

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        self.title = "Mum or dad"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left", type: .plain, goal: nil, motion: nil)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Proper", type: .plain, goal: nil, motion: nil)
        
        self.navigationController?.navigationBar.backgroundColor = .orange
        
        let ubtton = UIButton(sort: .system) // Utilizing .system for higher default look
        ubtton.body = self.view.bounds
        ubtton.setTitle("PRESENT", for: .regular)
        ubtton.titleLabel?.font = .systemFont(ofSize: 22, weight: .daring)
        ubtton.addTarget(self, motion: #selector(showTestController), for: .touchUpInside)
        self.view.addSubview(ubtton)
    }
    
    // MARK: - Repair for Dismissal Glitch
    // This tells the view controller to re-query its standing bar preferences
    // when it's about to reappear on the display screen.
    override func viewWillAppear(_ animated: Bool) {
        tremendous.viewWillAppear(animated)
        setNeedsStatusBarAppearanceUpdate()
    }
    
    @objc
    func showTestController() {
        let viewController = PresentedViewController()
        let navigationController = UINavigationController(rootViewController: viewController)
        navigationController.modalPresentationStyle = .fullScreen
        
        self.current(navigationController, animated: true)
    }
}

PresentedViewController.swift

Add the viewWillAppear methodology right here as properly. This can repair the preliminary structure problem with the additional area within the navigation bar.

Swift

import UIKit

class PresentedViewController: UIViewController {

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        self.view.backgroundColor = .systemRed
        self.title = "Introduced"
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Shut", type: .plain, goal: self, motion: #selector(shut))
    }
    
    // MARK: - Repair for Preliminary Presentation Glitch
    // By calling this right here, we make sure the system evaluates the standing bar state
    // through the presentation transition, permitting the navigation bar to structure appropriately.
    override func viewWillAppear(_ animated: Bool) {
        tremendous.viewWillAppear(animated)
        setNeedsStatusBarAppearanceUpdate()
    }
    
    @objc func shut() {
        self.dismiss(animated: true)
    }
    
    // This stays the supply of reality for hiding the standing bar
    override var prefersStatusBarHidden: Bool {
        return true
    }
}

## Why This Works

  • prefersStatusBarHidden: This computed property is how a view controller declares its desire for the standing bar’s visibility. Nevertheless, the system solely checks this property at sure occasions.

  • setNeedsStatusBarAppearanceUpdate(): This methodology tells the system, “My standing bar preferences might need modified, please verify them once more now.” It invalidates the present look and triggers the system to name prefersStatusBarHidden on the suitable view controller once more, inflicting it to replace the UI appropriately.

By inserting this name in viewWillAppear(_:) for each view controllers, you make sure that simply earlier than each seems on display screen, the system is compelled to get the newest standing bar state and regulate the structure accordingly, fixing each the preliminary presentation and the ultimate dismissal glitches.

LEAVE A REPLY

Please enter your comment!
Please enter your name here