在AppDelegate中设置的背景图片无法显示的技术问题咨询
Hey there, let's walk through why your background image isn't showing up on the simulator—this is a common issue with a few straightforward fixes. Here's what to check step by step:
1. Verify Your Image Resource is Accessible
First, let's rule out the most obvious culprit: the image itself.
- Double-check that
mainbg.jpgis actually in your project. If you're using an asset catalog (Assets.xcassets), make sure the image set is named exactlymainbg(no typos, and watch out for iOS's case sensitivity—MainBG.jpgis different frommainbg.jpg). - If the image is directly in your project folder (not in assets), open the File Inspector in Xcode and confirm the Target Membership box for your app is checked. Without this, the image won't be included in your build.
- Test if the image loads at all by adding a print statement:
If this printsprint(UIImage(named: "mainbg"))nil, your image isn't being found—fix the resource issue first before moving on.
2. Fix Window Initialization & Frame Logic
iOS 13+ Note:
If your app targets iOS 13 or later, the window property in AppDelegate isn't automatically initialized anymore—SceneDelegate takes over window management. If you're still trying to set this up in AppDelegate, you'll need to manually create the window:
if #available(iOS 13.0, *) { window = UIWindow(frame: UIScreen.main.bounds) // Don't forget to set your root view controller here! window?.rootViewController = YourRootViewController() }
Frame Calculation:
Your current frame uses self.window!.bounds.height * 1.688 for width—make sure this makes sense for your image's aspect ratio. Also, use UIScreen.main.bounds (or windowScene.bounds in SceneDelegate) to get reliable screen dimensions instead of relying on the window's bounds before it's fully initialized.
3. Configure the UIImageView Properly
A UIImageView won't display correctly without the right content settings:
- Set the
contentModeto ensure your image scales properly. For a background,.scaleAspectFillis usually best (it fills the view while maintaining aspect ratio):backgroundImg.contentMode = .scaleAspectFill backgroundImg.clipsToBounds = true // Add this to crop any parts of the image that go outside the frame - Make sure you're setting the image correctly with
UIImage(named:)—this method automatically handles 2x/3x retina variants, whereascontentsOfFilerequires a full path which is error-prone.
4. Add the ImageView to the Window Correctly
If you just use window?.addSubview(backgroundImg), it might get covered by your root view controller's view. Instead, insert it at the bottom of the window's subview stack:
window?.insertSubview(backgroundImg, at: 0)
Also, ensure you're calling window?.makeKeyAndVisible() after setting up the window and subviews.
Full Corrected Code Example
For AppDelegate (iOS 12 or earlier, or manual window setup):
@objc let backgroundImg = UIImageView() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Manually initialize window for iOS 13+ if needed if #available(iOS 13.0, *) { window = UIWindow(frame: UIScreen.main.bounds) window?.rootViewController = UIViewController() // Replace with your actual root VC } guard let window = window else { return true } let screenBounds = UIScreen.main.bounds backgroundImg.frame = CGRect(x: 0, y: 0, width: screenBounds.height * 1.688, height: screenBounds.height) backgroundImg.image = UIImage(named: "mainbg") backgroundImg.contentMode = .scaleAspectFill backgroundImg.clipsToBounds = true window.insertSubview(backgroundImg, at: 0) window.makeKeyAndVisible() return true }
For SceneDelegate (iOS 13+ recommended):
class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? let backgroundImg = UIImageView() func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = scene as? UIWindowScene else { return } window = UIWindow(windowScene: windowScene) window?.rootViewController = UIViewController() // Replace with your actual root VC let screenBounds = windowScene.bounds backgroundImg.frame = CGRect(x: 0, y: 0, width: screenBounds.height * 1.688, height: screenBounds.height) backgroundImg.image = UIImage(named: "mainbg") backgroundImg.contentMode = .scaleAspectFill backgroundImg.clipsToBounds = true window?.insertSubview(backgroundImg, at: 0) window?.makeKeyAndVisible() } }
Final Checks
- Run your app again after making these changes. If the image still doesn't show, use Xcode's Debug View Hierarchy to inspect the window's subviews—you can see if the
backgroundImgis present, what its frame looks like, and if it's being covered by another view.
内容的提问来源于stack exchange,提问作者Burak Çavuşoğlu

