Navigation Controller失效:嵌入含ScrollView的VC后无法修改属性
Hey there, let's break down why your navigation bar is stuck as dark gray and won't respond to your changes—here are the key issues and fixes:
1. You're Missing super.viewWillAppear(animated)
This is probably the biggest culprit! If you don't call the superclass implementation of viewWillAppear, the navigation controller's lifecycle hooks won't fire properly, which means your custom settings get ignored.
Update your viewWillAppear method to include the super call, then add explicit navigation bar styling:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Don't skip this! // Force-set navigation bar properties to override any Storyboard/global defaults navigationController?.navigationBar.barTintColor = .systemGray6 // Light gray (system default) navigationController?.navigationBar.isTranslucent = true // Match system's default behavior navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.label] navigationController?.navigationBar.tintColor = .systemBlue // For buttons/back arrow // Your existing child VC setup code (with safer optional binding) if let broadcastVC = storyboard?.instantiateViewController(withIdentifier: "Broadcast") { addChild(broadcastVC) broadcastVC.view.frame = scrollView.bounds scrollView.addSubview(broadcastVC.view) broadcastVC.didMove(toParent: self) } scrollView.contentSize = CGSize(width: view.frame.width, height: view.frame.height) scrollView.contentOffset = CGPoint(x: view.frame.width, y: view.frame.height) }
2. Check Storyboard Navigation Controller Settings
Double-check your Navigation Controller's attributes in Storyboard—someone (or a default setting) might have set it to dark gray:
- Select the Navigation Controller in your Storyboard
- Go to the Attributes Inspector
- Verify
Bar Tintisn't set to a dark gray color - Ensure
Translucentis enabled (this is the system default, and disabling it makes colors appear more opaque/dark) - Check
Tint Colorif your navigation buttons are also misbehaving
- Verify
3. Watch for Global Navigation Bar Styles
If your app has global navigation bar settings (in AppDelegate or SceneDelegate), those will override per-VC changes. Look for code like this:
UINavigationBar.appearance().barTintColor = .darkGray
If you find it, either adjust the global style or keep your per-VC settings to override it.
4. Child View Controller Might Be Overriding Navigation Settings
If your Broadcast view controller sets its own navigationItem properties, it could be interfering with the parent VC's navigation bar. To fix this:
- Either remove any
navigationItemcode from theBroadcastVC (let the parent VC manage the navigation bar) - Or, if you need the child VC to control the navigation bar, add this line after instantiating it:
Just note you'll need to reset this if you switch to other child VCs later.self.navigationItem = broadcastVC.navigationItem
Bonus: Improve Child VC Setup Safety
Avoid force-unwrapping (!) in your code—it can lead to crashes if the VC fails to instantiate. Use optional binding like I did in the updated viewWillAppear code above.
内容的提问来源于stack exchange,提问作者mazenqp

