Skip to content

Add nil check in updateViewHierarchy before adding to window#1158

Open
AdrianCurtin wants to merge 3 commits intoSVProgressHUD:masterfrom
AdrianCurtin:fix/updateViewHierarchy-nil-check
Open

Add nil check in updateViewHierarchy before adding to window#1158
AdrianCurtin wants to merge 3 commits intoSVProgressHUD:masterfrom
AdrianCurtin:fix/updateViewHierarchy-nil-check

Conversation

@AdrianCurtin
Copy link
Copy Markdown

Summary

Fixes #1157

Adds a nil check for rootViewController before adding the control view to the window in updateViewHierarchy.

Problem

When SVProgressHUD is shown, it adds its control view to the front window. If the window exists but has no rootViewController (or it's in an invalid state during app transitions), UIKit throws an NSInternalInconsistencyException when resolving trait collection for the new subview.

Solution

Check that window.rootViewController is not nil before adding the subview:

UIWindow *window = self.frontWindow;
if (window.rootViewController) {
    [window addSubview:self.controlView];
}

Testing

  • Verified the fix compiles correctly
  • If no valid window is available, the HUD simply won't be shown (graceful degradation)

Added a nil check before calling setNeedsStatusBarAppearanceUpdate on rootViewController to prevent potential crashes when rootViewController is not set.
Copilot AI review requested due to automatic review settings December 24, 2025 18:35
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds defensive nil checks for rootViewController to prevent crashes when SVProgressHUD attempts to add its view to windows or update status bar appearance during app state transitions.

Key Changes:

  • Added nil check for rootViewController before adding control view to window in updateViewHierarchy
  • Added nil check for rootViewController before calling setNeedsStatusBarAppearanceUpdate during HUD dismissal

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread SVProgressHUD/SVProgressHUD.m Outdated
AdrianCurtin added a commit to commandpostsoft/SVProgressHUD that referenced this pull request Dec 24, 2025
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash in updateViewHierarchy when window has no rootViewController

2 participants