带ScrollView的ViewController返回按钮无法移除,求有效解决方法
Hey there! Let’s figure out why that back button won’t disappear even after you added those lines in viewDidLoad(). When working with a ViewController that has a ScrollView, there are a few common pitfalls that might be blocking your progress. Here are actionable fixes to try:
1. Move your code to viewWillAppear(_:) instead of viewDidLoad()
The viewDidLoad() method runs too early in the view lifecycle—sometimes the navigation bar hasn’t fully initialized its state yet, especially if this ViewController was pushed onto the stack. Shifting your code to viewWillAppear ensures the navigation bar is fully set up when you modify it:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Hide the back button navigationItem.hidesBackButton = true // Disable swipe-to-go-back gesture (optional, if you need it) navigationController?.interactivePopGestureRecognizer?.isEnabled = false }
2. Check if the previous ViewController is controlling the back button
Here’s a key detail: the back button you see is actually owned by the previous ViewController in the navigation stack, not the current one. If the prior VC set a custom backBarButtonItem, it’ll override your current VC’s settings. To fix this, you have two options:
- Adjust the parent ViewController when pushing:
When you push your target VC from the previous screen, set an empty back bar button item there:// In the previous ViewController's push logic let targetVC = YourScrollViewController() // Set an empty title to hide the back button text/button navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) navigationController?.pushViewController(targetVC, animated: true) - Force override in the current VC:
Explicitly set an empty left bar button item to replace the default back button:navigationItem.leftBarButtonItem = UIBarButtonItem() // Empty button to take its place navigationItem.hidesBackButton = true
3. Rule out ScrollView content inset interference
If your ScrollView uses automatic content inset adjustment, it might cause unexpected layout shifts that affect the navigation bar’s visibility. Try adjusting this setting for your ScrollView (either in Interface Builder or code):
scrollView.contentInsetAdjustmentBehavior = .never
Note: If you still want the ScrollView to respect the navigation bar’s frame, use .scrollableAxes instead—this ensures insets adjust only when scrolling, without messing with navigation bar state.
4. Verify you’re modifying the correct navigation item
If your ViewController is embedded in a container (like a tab bar controller or another navigation controller), you might be targeting the wrong navigationItem. Double-check that self.navigationController isn’t nil, and try directly modifying the navigation bar’s top item:
navigationController?.navigationBar.topItem?.hidesBackButton = true
Give these steps a shot—most of the time, moving the code to viewWillAppear or fixing the previous VC’s back button setup resolves this issue for ScrollView-enabled ViewControllers.
内容的提问来源于stack exchange,提问作者J Duh

