Xcode9.2打开Xcode7.3项目报错:No known instance method for selector setNeedsStatusBarAppearanceUpdate
Hey there, let's work through this error you're hitting after moving your Xcode 7.3 project to Xcode 9.2! The issue boils down to a shift in how iOS handles status bar appearance updates in newer SDK versions.
Why This Error Pops Up
Back in older iOS versions (like iOS 7, which your code checks for), sending setNeedsStatusBarAppearanceUpdate directly to a UIWindow might have worked as a hack. But starting from later iOS releases (and enforced by the iOS 11 SDK used in Xcode 9.2), this method is exclusively part of UIViewController — UIWindow no longer implements this selector, which is why the compiler throws that error.
The Solution
Instead of calling the method on the window, you need to target your app's root view controller, since that's the component Apple designates to manage status bar appearance. Here's how to update your code:
- (void)setStatusBarHidden:(BOOL)statusBarHidden { _statusBarHidden = statusBarHidden; if ([[UIDevice currentDevice] tw_isRunningiOS7OrLater]) { // Grab the key window's root view controller (more reliable than firstObject) UIViewController *rootVC = [[[UIApplication sharedApplication] keyWindow] rootViewController]; // Call the update method on the view controller instead of the window [rootVC setNeedsStatusBarAppearanceUpdate]; } }
Quick Extra Tip
Make sure your root view controller (or any view controller responsible for status bar state) properly implements methods like prefersStatusBarHidden to reflect the _statusBarHidden value you're setting. This ensures the update actually triggers the correct status bar change.
内容的提问来源于stack exchange,提问作者Vinu Jacob

